2012-07-30 87 views
0

我有一個列表,其中有約700個項目需要在將它們添加到我的網頁之前編輯。 我嘗試手動編輯每個項目,但它變得太廣泛了,我想我可能會使用Java來讀取和編輯文件,因爲需要編輯的單詞在每個項目中具有相同的開始和結束。Java如何讀取和刪除文件的內容。

我想我會通過循環遍歷Q中的單詞開始,保存它,當我有邏輯工作時,我會發現如何讀取文本文件並再次執行相同的操作。 (如果有任何其他方式,我可以提供建議) 這裏是我放到一起的代碼,很久以前我用Java編碼,所以我現在基本上沒有技能。

import javax.swing.JOptionPane; 

public class CustomizedList 
{ 

public static void main (String[] args) 
{ 
    String Ord = JOptionPane.showInputDialog("Enter a word"); 
    String resultatOrd =""; 

    for(int i = 0; i < Ord.length(); i++) 
    { 
     if(Ord.charAt(i) == 'y' && Ord.charAt(i) == 'e' && Ord.charAt(i) ==  

's') 
     { 
      resultatOrd += Ord.charAt(i); 
      System.out.println(resultatOrd); 
     } 

     else 
     System.out.println("Wrong word."); 
    } 
} 
} 

我不知道我在做什麼錯,但是我輸入的字不符合邏輯地工作。 我想從這個文本文件中刪除兩個單詞:YES和NO,都是大寫和小寫。

+1

作爲@Christian說,一個字不能同時等於「Y」和「E」和「S」都在同一時間。角色在給定時間點只有一個值。但是,爲什麼不嘗試在String類中使用equalsIgnoreCase()方法。 – crowne 2012-07-30 17:29:12

回答

5

您的代碼不可能是正確的:

if(Ord.charAt(i) == 'y' && Ord.charAt(i) == 'e' && Ord.charAt(i) == 's') 

總是

解決方案:(糟糕,但仍是正確的

Ord.toLower().contains("yes") 

或者您案件):

if(Ord.charAt(i) == 'y' && Ord.charAt(i+1) == 'e' && Ord.charAt(i+2) == 's') 

如果你只是尋找平等的,你可以使用equals()

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#contains(java.lang.CharSequence

+0

它應該如何?請不要顯示 – user1535882 2012-07-30 17:23:52

+0

更改我的代碼:你在尋找eqal字符串,還是你在尋找一個子字符串? – 2012-07-30 17:45:57

1

if測試:

if(Ord.charAt(i) == 'y' && Ord.charAt(i) == 'e' && Ord.charAt(i) == 's') 

永遠不會爲真。你指定同一個角色必須是三個不同的東西。

在方法String.equalsIgnoreCase上取得更好的方法來測試所需的單詞。

例如:

if (word.equalsIgnoreCase("yes") || word.equalsIgnoreCase("no")) 
    // do something with word 
+0

換句話說,我每次都用一個新角色覆蓋Ord? – user1535882 2012-07-30 17:27:08

+0

不要覆蓋,只需要測試同一個地方的3個不同的char值並使用'&&':它只能是其中的一個,而不是全部三個 – pb2q 2012-07-30 17:27:46

+0

Okey謝謝。你知道如何閱讀這個條件的文件?並刪除它發現的每一個點的單詞! – user1535882 2012-07-30 17:31:39

0

希望這會有所幫助,我試圖評論各部分給你一個什麼樣的每一行做的想法。 只有當「是」和「否」分別在他們自己的行上時纔有效。

下面是I/O的Java教程鏈接。我建議你閱讀它時,你有時間,很多很好的信息Java I/O Tutorial

import java.io.*; 
import java.util.ArrayList; 
public class test { 

    public static void main(String[] args) throws Exception { 
    //name of file to read 
    File file = new File("filename.txt"); 

    //BufferedReader allows you to read a file one line at a time 
    BufferedReader in = new BufferedReader(new FileReader(file)); 

    //temporary Array for storing each line in the file 
    ArrayList<String> fileLines = new ArrayList<String>(); 

    //iterate over each line in file, and add to fileLines ArrayList 
    String temp=null; 
    while((temp=in.readLine())!=null){ 
     fileLines.add(temp);   
    } 
    //close the fileReader 
    in.close(); 

    //open the file again for writing(deletes the original file) 
    BufferedWriter out = new BufferedWriter(new FileWriter(file)); 

    //iterate over fileLines, storing each entry in a String called "line" 
    //if line is equal to "yes" or "no", do nothing. 
    //otherwise write that line the the file 
    for(String line : fileLines){ 
     if(line.equalsIgnoreCase("yes")||line.equalsIgnoreCase("no")){ 
      continue;//skips to next entry in fileLines 
     } 
     //writes line, if the line wasn't skipped 
     out.write(line); 
     out.write(System.getProperty("line.separator")); //newline 
    } 
    //save the new file 
    out.close(); 

    } 

}