2012-07-30 3 views
0

내 웹 페이지에 추가하기 전에 편집해야 할 약 700 개의 항목이있는 목록이 있습니다. 각 항목을 수동으로 편집 해 보았지만 너무 광범위 해졌습니다. 편집해야하는 단어의 시작과 끝이 각 항목에서 동일하므로 파일을 읽고 편집하는 대신 Java를 사용할 수 있다고 생각했습니다.Java 파일의 내용을 읽고 삭제하는 방법.

내가 Q에서 단어를 반복하면서 시작한다고 생각했는데, 그것을 저장하면 논리가 작동 할 때 텍스트 파일을 읽고 동일한 작업을 다시 수행하는 방법을 알게되었습니다. (나는 다른 방법이 있다면 제안을 열어 둔다.) 나는 지금까지 함께 작성한 코드를 제공한다. 나는 오랫동안 자바로 코딩했기 때문에 기본적으로 지금은 기술이 없다.

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

는 말했다 좋은 정보를 많이 추천 문자가 동시에 동일 할 수 없다 'Y'를 동시에와 '전자'와 '의'모든. 문자는 특정 시점에서 하나의 값만 갖습니다. 그러나 String 클래스에서 equalsIgnoreCase() 메서드를 사용하지 않으려는 이유는 무엇입니까? – crowne

답변

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) == 'e' && Ord.charAt(i) == 's') 

사실이 없을 것 : 당신은 그냥 평등을 찾고 있다면

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

+0

을 변경하십시오 : eqal 문자열을 찾으십니까? 아니면 하위 문자열을 찾으십니까? –

1

귀하의 if 테스트를 사용할 수 있습니다. 같은 캐릭터가 세 가지 다른 것임을 명시하고 있습니다.

원하는 단어를 테스트하는 더 좋은 방법은 String.equalsIgnoreCase 방법을 알아보십시오. 예를 들어

:

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

즉, 매번 Ord에 새로운 문자를 덮어 쓰고 있습니까? – user1535882

+0

전혀 덮어 쓰지 않고, 3 개의 다른 char 값에 대해 같은 위치를 테스트하고'&& '를 사용하면됩니다 : 그 중 하나 일 뿐이며 모든 세 가지가 될 수는 없습니다 – pb2q

+0

Okey thanks. 이 상태로 파일을 읽는 법을 아십니까? 발견 된 모든 지점에서 단어를 삭제하십시오! – user1535882

0

희망이 당신에게 각 행이 무엇의 아이디어를 제공하기 위해 각 부분을 언급하려고하는 데 도움이됩니다. "예"와 "아니오"가 각각 다른 줄에있는 경우에만 작동합니다.

다음은 입출력을위한 Java 자습서 링크입니다. 당신은 시간이있을 때 나는 그것을 읽는 @Christian으로 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(); 

    } 

} 
관련 문제