十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
總的來說,其作用就是統(tǒng)計該文件中包含word關(guān)鍵字的個數(shù)。
成都創(chuàng)新互聯(lián)專注于宿遷網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供宿遷營銷型網(wǎng)站建設(shè),宿遷網(wǎng)站制作、宿遷網(wǎng)頁設(shè)計、宿遷網(wǎng)站官網(wǎng)定制、微信小程序服務(wù),打造宿遷網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供宿遷網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
實際運行情況,擬定關(guān)鍵字word就是“word”,1.txt中的內(nèi)容為:
輸出結(jié)果:
該程序的作用:進到指定目錄,輸出這個目錄以及子目錄下所有文件的文件名,通過\t的行數(shù)來區(qū)別層級。
為了測試,我建立了這么一些文件:
D:\test
D:\test\test1
D:\test\test2
D:\test\test3.txt
D:\test\test1\t1.txt
D:\test\test1\t2.txt
D:\test\test2\t3.txt
其中,根路徑為D:\test,其level為0;
level1中包含文件夾test1、文件夾test2和文件test3;
level2中包含文件t1.txt(屬于文件夾test1)、文件t2.txt(屬于文件夾test1)、文件t3.txt(屬于文件夾test2)。
則該程序的輸出為:
其中:
t1.txt前有1行\(zhòng)t,說明t1.txt位于第2層級
t2.txt前有1行\(zhòng)t,說明t2.txt位于第2層級
t3.txt前有1行\(zhòng)t,說明t3.txt位于第2層級
test3.txt前沒有\(zhòng)t,說明test3.txt位于第1層級
符合預(yù)期。
另外為了再明顯一些,我給輸出再加點東西,這樣會不會更明顯一些:
其中,t1.txt位于的大括號中有一行\(zhòng)t和它的名字,表明了它確實在第2層級中,t2、t3同t1;test3.txt位于的大括號中只有它的名字表明了它確實在第1層級中
【俊狼獵英】團隊為您解答~
想法是沒有錯的,錯在28~31行的else
要在內(nèi)層for循環(huán)遍歷完字母表之后才能判斷輸入是否正確,而不是檢驗一個字母就去做判斷
按照上面的代碼,比如輸入b,檢驗完不等于'a'或'A',馬上返回輸入錯誤
要改的話可以這樣
從24行開始
flag = false;
for (int j = 1; j = big.length; j++){
if (chr == small[j]){
System.out.print(big[j]);
flag = true;
}
else if(chr == big[j]){
System.out.print(small[j]);
flag = true;
}
}
if(!flag){
System.out.println("重新輸入");
}
百度沒法縮進,你粘出去縮進一下應(yīng)該更清楚
不過這個邏輯無論是代碼長度還是執(zhí)行效率顯然比下面要差多了
放eclipse里就會有紅線提示,類名和文件名應(yīng)該一致
你說的不會報錯不知道是什么情況,默認(rèn)設(shè)置是會報錯的
import java.util.Arrays;
import java.util.Comparator;
public class A {
public static void main(String[] args) {
String[] ls={"A","B","C","V","G","H","K"};
final String[] ses={"B","G","K"};
Arrays.sort(ls, new ComparatorString(){
public int compare(String str1, String str2) {
if(str1.equals(str2)){
return 0;
}
if(str1.equals(ses[0])){
return -1;
}else if(str1.equals(ses[1])){
if(str2.equals(ses[0])){
return 1;
}
return -1;
}else if(str1.equals(ses[2])){
if(str2.equals(ses[0]) || str2.equals(ses[1])){
return 1;
}
return -1;
}else if(str2.equals(ses[0]) || str2.equals(ses[1]) || str2.equals(ses[2])){
return 1;
}
return 0;
}
});
for(String item: ls){
System.out.println(item);
}
}
}
是否可以解決您的問題?
public?static?void?main(String[]?args)?{
try{
File?file?=?new?File("D:\\123.txt");
FileInputStream?inputStream?=?new?FileInputStream(file);
BufferedReader?bufferReader?=?new?BufferedReader(new?InputStreamReader(inputStream,?"GBK"));
ListString?threadColumns?=?new?ArrayListString();
while(true){
String?line?=?bufferReader.readLine();
if(line?==?null?||?"".equals(line))
break;
else{
String?threadColum?=?line.split("\t")[2].toString().substring(2,9);
threadColumns.add(threadColum);
}
}
bufferReader.close();
inputStream.close();
Collections.sort(threadColumns);
File?outFile?=?new?File("D:\\456.txt");
FileWriter?fw?=?new?FileWriter(outFile);
BufferedWriter?writer?=?new?BufferedWriter(fw);
for(String?threadColum?:?threadColumns){
writer.write(threadColum);
writer.newLine();//換行
}
writer.close();
fw.close();
}catch(Exception?e){
e.printStackTrace();
}
}