2016-03-28 19 views
-1

Üç metin dosyası oluşturdum, inputt.txt, outputt.txt, & errorr.txt. Inputt.txt giriş içindir, outputt.txt dosyasının, geçerli tamsayılar olan inputt.txt dosyasındaki tüm sayıları yazdırması gerekir ve errorr.txt, giriş metni dosyasındaki tüm hataları yazdırır. Yinelemede sorun yaşıyorum. System.out.println tüm sayıları inputt.txt dosyasından sadece iyi bir şekilde basar; ancak, tüm sayıları outputt.txt dosyasına yazdırmaya çalıştığımda, yalnızca ilk sayı satırını yazdırır. Benim kodum:Başka bir metin dosyasına yazdırmak için bir metin dosyası üzerinden nasıl yineleyebilirim?

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.PrintStream; 
import java.io.PrintWriter; 
import java.util.InputMismatchException; 
import java.util.NoSuchElementException; 
import java.util.Scanner; 

public class Lab5_Exceptions_Redo extends Exception { 

    public static void main(String[] args) throws FileNotFoundException { 

     String file = "inputt.txt"; 
     String file1 = "outputt.txt"; 
     String file2 = "errorr.txt"; 

     PrintWriter errorr = new PrintWriter(file2); 
     PrintStream ps = new PrintStream(file2); 

     PrintWriter outputt = new PrintWriter(file1); 
     Scanner scan = new Scanner(new File(file)); 
     while (scan.hasNext()) { 
      try { 

       String number = scan.nextLine(); 
       int num = Integer.parseInt(number); 
       System.out.println(num); 
       outputt.println(num); 
      } catch (InputMismatchException e) { 
       errorr.println("There was an input mismatch error."); 
       errorr.println(e.getMessage()); 
       e.printStackTrace(ps); 
      } catch (NoSuchElementException e) { 
       errorr.println("There is no such element."); 
       errorr.println(e.getMessage()); 
       e.printStackTrace(ps); 
      } catch (UnsupportedOperationException e) { 
       errorr.println("An unsupported operation occured."); 
       errorr.println(e.getMessage()); 
       e.printStackTrace(ps); 
      } catch (NumberFormatException e) { 
       errorr.println("Number Format Exception."); 
       errorr.println(e.getMessage()); 
       e.printStackTrace(ps); 
      } 

      errorr.close(); 
      outputt.close(); 
     } 
     scan.close(); 
    } 
} 
+1

Döngüde 'outputt' öğesini kapatıyorsunuz. Düzeltmek için dışarı taşı. Yazım hatası olarak kapatmak için oylama. – dasblinkenlight

+0

Dışarıdan kapatamıyorum çünkü içeride değişkenleri bildiriyorum ve değişkeni dışarıda taşıyamam çünkü bunlar benim denememde olmalı. – HelpMe

cevap

0

PrintWriter akış değişkeni errorr for döngüsü dışında hareket etmelidir. Aşağıdaki kod sürümünü deneyin.

public class Lab5_Exceptions_Redo extends Exception { 

public static void main(String[] args) throws FileNotFoundException { 

    String file = "inputt.txt"; 
    String file1 = "outputt.txt"; 
    String file2 = "errorr.txt"; 

    PrintWriter errorr = new PrintWriter(file2); 
    PrintStream ps = new PrintStream(file2); 

    PrintWriter outputt = new PrintWriter(file1); 
    Scanner scan = new Scanner(new File(file)); 
    while (scan.hasNext()) { 
     try { 
      String number = scan.nextLine(); 
      int num = Integer.parseInt(number); 
      System.out.println(num); 
      outputt.println(num); 
     } catch (InputMismatchException e) { 
      errorr.println("There was an input mismatch error."); 
      errorr.println(e.getMessage()); 
      e.printStackTrace(ps); 
     } catch (NoSuchElementException e) { 
      errorr.println("There is no such element."); 
      errorr.println(e.getMessage()); 
      e.printStackTrace(ps); 
     } catch (UnsupportedOperationException e) { 
      errorr.println("An unsupported operation occured."); 
      errorr.println(e.getMessage()); 
      e.printStackTrace(ps); 
     } catch (NumberFormatException e) { 
      errorr.println("Number Format Exception."); 
      errorr.println(e.getMessage()); 
      e.printStackTrace(ps); 
     } 
    } 
    errorr.close(); 
    outputt.close(); 
    scan.close(); 
} 
} 
İlgili konular