2016-05-04 13 views
1

私のプログラムはZuessファイルを読むことができるはずですファイルを開いてJavaの最初の単語を削除する

私を見てください!
私を見てください!
今すぐ見る
楽しいことが楽しいです
しかし、あなたは
を知っています。

各行の最初の単語を削除しながら新しいファイルを作成します。プログラムを実行すると、最初の単語が削除されます。

Courier;} 





\cf0 \expnd0\expndtw0\kerning0 
at me!\ 
at me!\ 
at me NOW!\ 
is fun to have fun\ 
you have\ 
know how.\ 

私はNEWFILE.TXTだけではない、誰かが助けてくださいすることができますファイルに実際にあるものを手に入れるファイル内にあるものを印刷しようとすると、それは最後に、書き込み何です。

//Program that removes the first word from every line in a file 

import java.io.*; 
import java.util.Scanner; 

public class test { 

    //Main Method 
    public static void main (String args[]) 
    { 

     //Get the system defined 
     String newLine = System.getProperty("line.separator"); 

     //Name of original file 
     String origFileName = "zuess.RTF"; 

     //Name of new file 
     String newFileName = "NewFile.txt"; 

     //Creating the PrintWriter 
     PrintWriter outputStream = null; 
     try { 
      outputStream = new PrintWriter 
        (new File(newFileName)); 
     } catch (FileNotFoundException e1) { 
      System.out.println("Error opening the file " + origFileName); 
      System.exit(0); 
     } 


     //Create scanner 
     Scanner inputStream = null; 
     try { 
      inputStream = new Scanner 
        (new File(origFileName)); 
     } catch (FileNotFoundException e1) { 
      System.out.println("Error opening the file " + origFileName); 
      System.exit(0); 
     } 

     //While there's lines in the the file 
     while(inputStream.hasNextLine()) 

     { 
      //get line in original file 
      String line = inputStream.nextLine(); 
      //split line to get words 
      String[] arr = line.split(" "); 
      for(int i = 1; i < arr.length; i++) 
      { 
       //So the data will print in the new file 
       outputStream.write(arr[i] + " "); 
      } 
      //Line separator 
      outputStream.write(newLine); 
     } 
     //Close Stream 
     outputStream.close(); 
     inputStream.close(); 


     //catch exception 
     try 
     { 
      inputStream = new Scanner(new File (newFileName)); 
     } 
     catch(FileNotFoundException e) 
     { 
      System.out.println("Error opening the file " + newFileName); 
      System.exit(0); 
     } 

     System.out.println("The file " + newFileName + " Contains the following lines: "); 
     while (inputStream.hasNextLine()) 
     { 
      String text = inputStream.nextLine(); 
      System.out.println(newFileName); 
     } 
     inputStream.close(); 
    }//End main 
}//end class 
+0

をバージョンを使用している私はそれをしたいですただ書きなさい: 私に!私に ! 今すぐ! は楽しいです。 あなたは ノウハウを持っています。 –

+0

私を見て! 私を見て! 今見てください! 楽しみが楽しいです しかし、あなたはどのように知って 持っています。 –

+0

元のファイルの内容です。 –

答えて

0

この方法を試してみてください:ここで

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

    //Get the system defined 
    String newLine = System.getProperty("line.separator"); 

    //Name of original file 
    String origFileName = "new.txt"; 

    //Name of new file 
    String newFileName = "NewFile.txt"; 

    //Creating the PrintWriter 
    PrintWriter outputStream = null; 
    try { 
     outputStream = new PrintWriter 
       (new File(newFileName)); 
    } catch (FileNotFoundException e1) { 
     System.out.println("Error opening the file " + origFileName); 
     System.exit(0); 
    } 

    //Opening origin file and getting all lines 
    Path pathA = Paths.get(origFileName); 
    List<String> file = Files.readAllLines(pathA); 

    //for every line from origin file 
    for (String line: file) { 

     //split line to get words 
     String[] arr = line.split(" "); 
     for(int i = 1; i < arr.length; i++) 
     { 
      //So the data will print in the new file 
      outputStream.write(arr[i] + " "); 
     } 
     //Line separator 
     outputStream.write(newLine); 
    } 
    //Close Stream 
    outputStream.close(); 

} 
+0

それはまだ悲しいことに同じ方法でファイルを書いているか、またはファイルが常に書き込まれる方法です。 –

0

あなたはこれを試して行く:それは "zuess.txt" ではない "zuess.rtf"

package test; 
import java.io.*; 
import java.util.Scanner; 

public class test { 

    //Main Method 
    public static void main (String args[]) 
    { 
     String origFileName = "zuess.txt"; 
     String newFileName = "NewFile.txt"; 

     PrintWriter outputStream = null; 
     try { 
      outputStream = new PrintWriter 
        (new File(newFileName)); 
     } catch (FileNotFoundException e1) { 
      System.out.println("Error opening the file " + origFileName); 
      System.exit(0); 
     } 
     Scanner inputStream = null; 
     try { 
      inputStream = new Scanner 
        (new File(origFileName)); 
     } catch (FileNotFoundException e1) { 
      System.out.println("Error opening the file " + origFileName); 
      System.exit(0); 
     } 
     while(inputStream.hasNextLine()) 
     { 
      String line = inputStream.nextLine(); 
      String[] arr = line.split(" "); 
      for(int i = 1; i < arr.length; i++) 
       outputStream.write(arr[i]+" "); 
      outputStream.println(); 
     } 
     outputStream.close(); 
     inputStream.close(); 

     try 
     { 
      inputStream = new Scanner(new File (newFileName)); 
     } 
     catch(FileNotFoundException e) 
     { 
      System.out.println("Error opening the file " + newFileName); 
      System.exit(0); 
     } 

     System.out.println("The file " + newFileName + " Contains the following lines: "); 
     while (inputStream.hasNextLine()) 
     { 
      String text = inputStream.nextLine(); 
      System.out.println(text); 
     } 
     inputStream.close(); 
    }//End main 
}//end class 
+0

ありがとうございます。ファイルは常にその形式で書かれますか? ""と\ "の中のものは? "宅配便;}"!!! "\ CF0 \ expnd0 \ expndtw0 \ kerning0" 私の が\私の \ 私のNOW \ は、あなたが持っている \楽しみを持っている楽しいです\ 知っている方法\ –

+0

あなたはどういう意味ですか?テキストの単純な行で、あなたは私に与えました、私はちょうどそれらを含むtxtファイルを作っただけです。 – 3kings

+0

新しいファイルでは、常にCourierを書いています。 }と\ cf0 \ expnd0 \ expndtw0 \ kerning0 –

関連する問題