2013-12-17 5 views
2

Java로 경험이 부족하기 때문에 (지난 2 개월 동안 학교에서 만 학습 했음) 문제가 발생했습니다.Java에서 작은 텍스트 파일 다시 읽기

나는이 친구들을 농담으로 "모욕하는 기계"프로그램을 만들고 있었다. 이 프로그램은 텍스트 파일을 참조하고 텍스트 파일의 줄 수를 찾습니다. 그런 다음 1에서 최대 숫자를 생성하고 모든 텍스트를 배열에 저장하고 해당 행 번호의 텍스트를 인쇄합니다. 또한 사용자는 모욕을 만들 수 있으며 텍스트 파일을 새로운 모욕으로 업데이트합니다.

내가 모욕적 인 부분을 실행할 때 (나는 BufferedReader을 사용하고있다. 더 간단한 방법이있을 수있다.) 그 위치는 여전히 텍스트 파일의 맨 아래에 있으며 나는 길을 찾을 수 없다. 텍스트 파일을 다시 읽은 다음 배열을 업데이트하십시오. 모욕적 인 부분으로 돌아 가면 파일 끝에 도달하면 NULL이 인쇄됩니다. 이 문제를 해결하는 방법에 대한 아이디어가 있습니까?

그냥 참고로, 새로운 BufferedReader로를 만들에 댓글을 올리려면, 사용자가 앞뒤로 생성 및 핵심 프로그램의 갈 수 있습니다, 그래서 무한 BufferedReaders

필요 했어

(내가하지 않을 수 있습니다 내 코드를 인식 할 가장 효율적인은, 난 단지 라인을 사용하여 파일을 읽기 위해

import java.util.Scanner; 
import java.io.*; 
import java.util.Random; 
public class InsultingMachine{ 
    public static void main(String args[]) throws IOException{ 
     File insult = new File("insults.txt"); 
     boolean exist = insult.exists(); 

     if(exist == false){ 
      System.out.println("The file does not exist, creating the file...?"); 
      PrintWriter writer = new PrintWriter("insults.txt", "UTF-8"); 
      writer.close(); 
      System.out.println("Done"); 
     } 

     //objects 
     Scanner input = new Scanner(System.in); 
     Scanner inputIn = new Scanner(System.in); 
     Scanner inputIn2 = new Scanner(System.in); 
     Random ranGen = new Random(); 
     BufferedReader reader = new BufferedReader(new FileReader("insults.txt")); 
     BufferedReader reader2 = new BufferedReader(new FileReader("insults.txt")); 
     BufferedWriter wr = new BufferedWriter(new FileWriter("insults.txt", true)); 

     //vars 
     int i, ranInt; 
     int lineNum = 0; 
     int lineNum2 = 0; 
     int counter = 0, counter2 = 0, counter3 = 0, menuCounter = 0; 
     String outputting; 
     String s1, s2; 

     while (menuCounter == 0){ 
      System.out.println("Hello, please select an option from the menu: "); 
      System.out.println("(1) INSULTS"); 
      System.out.println("(2) Create insults"); 
      System.out.println("(3) Exit"); 

      i = input.nextInt(); 

      switch(i){ 
       case 1: 
        //Establish Line Number 

        while(reader.readLine() != null){ 
         lineNum++; 
        } 
        //store the lines of the text file 
        String L[] = new String[lineNum]; 

        lineNum2 = 0; 
        while(lineNum2 < lineNum){ 
         L[lineNum2] = reader2.readLine(); 
         lineNum2++; 
        } 

        System.out.println("You may start you fool...Enter an insult --type exit to exit-- :"); 
        while (counter == 0){ 
         s2 = inputIn.nextLine(); 

         if(s2.equalsIgnoreCase("exit")){ 
          break; 
         } 

         //generate number from 1 to max line number 
         ranInt = ranGen.nextInt(lineNum); 
         if (ranInt == 0) 
         { 
          ++ranInt; 
         } 

         System.out.println(L[ranInt]); 
        } 
        break; 

       case 2: 
        while(counter3 == 0){ 
         System.out.println("Please enter the insult you would like to add --type exit to exit-- : "); 
         s2 = inputIn2.nextLine(); 

         if(s2.equalsIgnoreCase("exit")) 
         { 
          wr.close(); 
          break; 
         } 
         wr.newLine(); 
         wr.write(s2); 
        } 
        break; 

       case 3: 
        menuCounter++; 
        break; 
       default: 
        System.out.println("Please enter a correct value..."); 
        break; 
      } 
     } 
    } 
} 

답변

0

) 프로그램을 시작했습니다 Apache Commons IO Library

apache commons io

FileUtils 클래스가 있습니다. FileUtils

그것은이 방법은 파일을 읽고 당신에게 라인의 목록을 반환 한 다음 그 List

당신이 원하는대로 할 수있는 것입니다 readLines(File f) readLines(File f)

방법을 가지고