2013-07-08 2 views
0

내 Java 콘솔 출력을 내 데스크탑의 .txt 파일에 쓰려고합니다. 하지만 그 방법이 시작되면 콘솔 출력이 있고 txt 파일이 만들어 지지만 비어있어 BufferedReader (in)가 작동하지 않는다는 것을 깨달았습니다 ... ("ok1 - i"문으로 인해)콘솔 출력을 .txt 파일로 작성하십시오.

질문은 무엇입니까 ?? 또는 내 코드에 무엇이 잘못 되었습니까 ?? 당신이 볼과 당신의 시간을

package noobxd; 

    import java.io.BufferedReader; 
    import java.io.BufferedWriter; 
    import java.io.FileWriter; 
    import java.io.IOException; 
    import java.io.InputStreamReader; 
    import java.util.Random; 

    public class Main { 

public static void main(String[] args) throws IOException { 
    String path = "C:\\Users\\Mario\\Desktop\\output.txt"; 

    generate_codes(); 

    writetxt(path); 
} 

private static void writetxt(String path) throws IOException { 
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 

    BufferedWriter out = new BufferedWriter(new FileWriter(path)); 
    try { 
     String inputLine; 
     inputLine = ""; 
     int i=0; 
     System.out.println("Starting"); 
     while (!inputLine.isEmpty()){ 
      System.out.println("ok1"+i); 
      inputLine = in.readLine(); 
      System.out.println("ok2"+i); 
      out.write(inputLine); 
      System.out.println("ok3"+i); 
      out.newLine(); 
      System.out.println("ok4"+i); 
      i++; 
     } 
     System.out.print("Write Successful"); 
    } catch (IOException e1) { 
     System.out.println("Error during reading/writing"); 
    } finally { 
     out.close(); 
     in.close(); 
    } 
} 

private static void generate_codes() { 
    Random rnd = new Random(); 
    for (int i = 0; i < 30; i++) { 
     int code = rnd.nextInt(201) + 100; 
     int students = rnd.nextInt(31) + 40; 
     int j = rnd.nextInt(4); 
     String type = new String(); 
     switch (j) { 
      case 0: 
       type = "Theory"; 
       break; 
      case 1: 
       type = "Lab"; 
       break; 
      case 2: 
       type = "Practice"; 
       break; 
      case 3: 
       type = "Exam"; 
       break; 
     } 
     System.out.println("TEL" + code + "-TopicNumber" + i + "-" + students + "-" + type); 

    } 
} 
} 

감사를 실행할 수 있도록이, 내 코드입니다, 내 문제를 해결하는 데 도움이 바랍니다.

+1

+1) –

+0

롤 hahah @UncleIroh이 기반 내 자바-SQL에서 사용되는 나를 txt 파일을 내보낼 수 있도록하는 "별도의 응용 프로그램은"이기 때문이다 앱 (주 하나, txt 파일을 가져올 수 있습니다.) 그래서 그냥 콘솔에 다음 noobxd – Mgustavoxd1

답변

2
String inputLine; 
inputLine = ""; 
... 
while (!inputLine.isEmpty()) // this is false and so the loop is not executed 

향후에는 디버깅 도구를 사용하는 법을 배우고 코드를 자세히 읽으십시오. 당신이 EOF까지 읽으려고하는 경우에는 사용자가 콘솔에 빈 줄을 입력 할 때까지 반복 유지하려면,

while ((inputLine = in.readLine()) != null) { 
    ... 
} 
+0

잘 명명 된 있지만이'(while (inputLine.isEmpty())'출력을 그냥 설정할 경우. '.. TEL221-TopicNumber27-55-Lab' 'TEL280-TopicNumber28-58-Practice' 'TEL174-TopicNumber29-51-Lab' ''Starting' ok10' – Mgustavoxd1

+0

@ user2161991 좋아? 나는 그것이 무엇을 해야하는지 모르겠다. ... – Zong

+0

메소드가 제대로 실행 되었다면 나를 보여주는 "플래그"와 같아서,이 출력 (그냥 ok1 0)을 사용한다. (라인 i = 0 일 때 처음으로 ok) (첫 번째 줄)) 나는 while 문을 입력했지만 inputLine = in.readLine() 메서드가 실행되지 않았다는 것을 알고있다. – Mgustavoxd1

0

를 사용하여, 당신은 아마

while (true) { 
     System.out.println("ok1"+i); 
     inputLine = in.readLine(); 
     if (inputLine.isEmpty()) 
      break; 
     // the rest of your loop 
} 
0

같은 당신 가능성은해야합니다 이 같은 수행합니다 noobxd 패키지 이름에 대한

inputLine = in.readLine(); 
while (inputLine != null && !inputLine.isEmpty()){ 
    System.out.println("ok1"+i); 
    System.out.println("ok2"+i); 
    out.write(inputLine); 
    System.out.println("ok3"+i); 
    out.newLine(); 
    System.out.println("ok4"+i); 
    i++; 
    inputLine = in.readLine(); 
} 
+0

나는 당신이 말한대로했지만 여전히'inputLine = in.readLine();'은 실행되지 않습니다, 나는 왜 그런지 모르겠습니다. – Mgustavoxd1

관련 문제