2012-07-30 86 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(); 

    } 

}