2017-05-24 9 views
-1

に別の文字列に出力を追加し続けるためにどのようにこれは私のコードである私は、ファイルの内容が一度に1行ずつ読み込まれるように、上記のコードを変更する必要があります。それを達成する方法?は、テキストファイルの内容を読み、異なる反復

EDIT:ファイルの内容を連結することはできません。メソッドは一度に1行を読み込んで返し、次に内容を別の文字列に追加する必要があります。

例: ファイルの内容は以下のとおりです。

/hello 
/goodbye 

1.first time the program run ,return /hello, which gets appended to another 
    string and is displayed to the user. 
2.If user select "ok" 
3.Go back to #1 and display the next string which is /goodbye. 
4. Keep iterating till the end of file. 
+0

テスト '場合(file.exists())...'一切不要です。ファイルが存在しない場合、前の 'Scanner in = new Scanner(file);からの' FileNotFoundException'のためにプログラムがすでに終了しているので、falseになることはありません。 –

答えて

0

このを検討してください、あなたのアイデアを取ると、あなたのプログラム(コメントで説明)にそれを適用することがあります。

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 

public class ReadLineByLineExample { 

    public static void main(String[] args){ 
     String path = "myTextFile.txt"; // some text file 
     File file = new File(path); // create the file object 

     // suppose this is the String that you want to add to from each line 
     String result = ""; 
     Scanner in; 

     try { 
      in = new Scanner(file); // instantiate the Scanner and set the source from the file 
      while(in.hasNextLine()){ // while this file has a line 
       // append the line from file to the String 
       // (you may want to remove the space between them " ") 
       result += in.nextLine() + " "; 
      } 
      in.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     // at the end test the result 
     System.out.println(result); 

     } 
} 

試験

ファイル内

テキスト:

First line 
Second line 
Third line 

出力:

First line Second line Third line 
+0

私はまだ答えを編集していましたが、あなたはただ投票を下したいと思っていました。 –

+0

@OmarAhmed Pardon、あなたは私と話しているのですか? – Yahya

-1

あなたが代わりにそうようにBufferedReaderのを使用することができます - これは、一度にそれぞれの行が、その後追加読み込み

import java.io.BufferedReader; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStreamReader; 

//Method start 
String line; 
String all =""; 
try{ 

File file = new File(ReadTextFile.class.getClassLoader().getResource("file/Url.txt").getFile()); 
FileInputStream filein = new FileInputStream(file); 
InputStreamReader is = new InputStreamReader(filein); 
BufferedReader br = new BufferedReader(is); 

     while((line=br.readLine())!= null){//You must notify the end of the file 

      all = all+line+"\n"; 
     } 
filein.close(); 
} catch (FileNotFoundException e) { 
e.printStackTrace(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 

内部のファイル全体all String .. BufferedReader i大きなファイルを処理する方が良い

0
  1. Scanner.Use BufferedReaderは使用しないでください。

  2. str + ""を使用する代わりにStringBuilder/StringBufferを使用します。

+0

あなたの答えといくつかのコード例 – Yahya

+0

に説明を与えることを検討してください。Omar Ahmedの答えと同じように、すべて=すべて+行+ "\ n"をStringBuilderに置き換えてください。 – SpringDRen

0

私は私の問題を解決することができました。皆さんが指針を提供してくれたおかげです。

これは私がやったことです:

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

    contactenateContentsWithUrl("google.com"); 

} 

private static ArrayList<String> readContentsFromFile() throws FileNotFoundException { 
    /*this program loads the file contents and return them in an arraylist*/ 
    File file = new File(Main.class.getClassLoader().getResource("resource/test.txt").getFile()); 
    Scanner in = new Scanner(file); 

    ArrayList<String> readContents=new ArrayList<>(); 

    while(in.hasNext()){ 
     readContents.add(in.next()); 
    } 
    in.close(); 

    System.out.println("Contents are " + readContents); 
    return readContents; 
} 

private static void contactenateContentsWithUrl(String url) throws FileNotFoundException { 


    ArrayList<String> lines=readContentsFromFile(); 
    StringBuilder formedUrl=new StringBuilder(); 

    for(int i=0;i<=lines.size()-1;i++){ 

     StringBuilder newStr= formedUrl.append(url).append(lines.get(i)); 
     System.out.println(newStr.toString()); 
     newStr.delete(0,newStr.length()); 

    } 

} 
関連する問題