2016-05-06 10 views
0

私はJavaを使い慣れていません。私はあまりにも確認されませんでしたJavaで書き込むときに事前定義されたテキストをファイルに設定します。

:私は、ファイルに詳細を記述しようとしているが、私は、次のようなファイルで定義済みのテキストが欲しい:
名:
年齢:
市:
スポーツをこれを行う方法。 switch文を使用しようとしていますが、配列の位置によっては事前に定義されたテキストが最初に書き込まれ、関連情報が書き込まれます。私はこの方法では、コードの行を減らすために、別の方法では、switch文を入れたかったが、私は、forループ

\t 
 
    private String setPredefinedFileLines() { 
 
\t int i = 0; String 
 
\t startOfFileText = null; switch (i) { 
 
\t case 1: startOfFileText = 
 
\t "Server Name : "; break; } 
 
    
 
\t return startOfFileText; 
 
    
 
\t }

public void writeStringToFile(String[] stringContents) throws IOException { 
 
\t \t BufferedWriter bw = new BufferedWriter(new FileWriter(fileToWriteTo)); 
 

 
\t \t try { 
 

 
\t \t \t for (int i = 0; i < stringContents.length; i++) { 
 
\t \t \t \t String startOfFileText = null; 
 
\t \t \t \t switch (i) { 
 
\t \t \t \t case 1: 
 
\t \t \t \t \t startOfFileText = "Server Name : "; 
 
\t \t \t \t \t break; 
 
\t \t \t \t } 
 
\t \t \t \t bw.write(stringContents[i]); 
 
\t \t \t \t bw.newLine(); 
 
\t \t \t \t bw.flush(); 
 
\t \t \t } 
 
\t \t } catch (IOException e) { 
 
\t \t \t System.out.println(e.getMessage()); 
 
\t \t } 
 

 
\t \t finally { 
 
\t \t \t if (bw != null) { 
 
\t \t \t \t bw.close(); 
 
\t \t \t } 
 
\t \t } 
 

 
\t }
内部ながら私を返す方法をあまりにも確認されませんでしたここで

答えて

1

完全な作業コードは

public class WriteToFile { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) throws Exception{ 
     File file = new File("file.out"); 
     BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); 
     List<Student> students = new ArrayList<Student>(); 
     Student s1 = new Student("Vivek", "23", "Pune", "Cricket"); 
     Student s2 = new Student("Vivek", "23", "Pune", "Cricket"); 
     Student s3 = new Student("Vivek", "23", "Pune", "Cricket"); 

     students.add(s1); 
     students.add(s2); 
     students.add(s3); 

     for(Student s: students){ 
      bw.write("Name:"+s.name+"\n"); 
      bw.write("Age:"+s.age+"\n"); 
      bw.write("City:"+s.city+"\n"); 
      bw.write("Sport:"+s.sport+"\n"); 
     } 


     bw.close(); 
    } 

} 

class Student{ 
    String name, age, city, sport; 

    public Student(String name, String age, String city, String sport){ 
     this.name = name; 
     this.age = age; 
     this.city = city; 
     this.sport = sport; 
    } 
} 
です

コードは非常に簡単ですが、理解できない場合はお知らせください。

+0

おかげで、私はすでに私が事前に定義された値で出力したいという点で、情報の配列を持っているので、私の現在の方法は何であるか –

+0

に動作しませんなぜ私があまりにも確認されませんでした名前、年齢、都市、1人の学生のスポーツが含まれている場合は 'bw.write(" Name: "+ stringContents [0])'などと書くことができます。 – viveksinghggits

0

これを試してください。

import java.io.*; 

public class SampleApp { 
    public static void main(String... args) throws IOException { 
     // For purposes of demonstration values are hardcoded. 
     String name = "Desmond"; 
     int age = 24; 
     String city = "New York"; 
     String sport = "Basketball"; 

     String fileName = "output.txt"; 

     writeToFile(name, age, city, sport, fileName); 
    } 


    public static void writeToFile(String name, int age, String city, String sport, String fileName) throws IOException { 
     File fileToWriteTo = new File(fileName); 
     fileToWriteTo.createNewFile(); 

     try (PrintWriter pw = new PrintWriter(fileToWriteTo)) { 
      pw.println("Name: " + name); 
      pw.println("Age: " + age); 
      pw.println("City: " + city); 
      pw.println("Sport: " + sport); 
     } 
    } 
} 

しかし、このように、別のクラスでは氏名、年齢、都市、そしてスポーツをバンドルする方が賢明です:

public class Info { 
    public String name; 
    public int age; 
    public String city; 
    public String sport; 


    Info(String name, int age, String city, String sport) { 
     this.name = name; 
     this.age = age; 
     this.city = city; 
     this.sport = sport; 
    } 
} 

、代わりにこれを行います。助けのための

import java.io.*; 

public class SampleApp { 
    public static void main(String... args) throws IOException { 
     // For purposes of demonstration values are hardcoded. 
     Info info = new Info("Desmond", 24, "New York", "Basketball"); 

     String fileName = "output.txt"; 

     writeToFile(info, fileName); 
    } 


    public static void writeToFile(Info info, String fileName) throws IOException { 
     File fileToWriteTo = new File(fileName); 
     fileToWriteTo.createNewFile(); 

     try (PrintWriter pw = new PrintWriter(fileToWriteTo)) { 
      pw.println("Name: " + info.name); 
      pw.println("Age: " + info.age); 
      pw.println("City: " + info.city); 
      pw.println("Sport: " + info.sport); 
     } 
    } 
} 
関連する問題