StudentTestクラスにてテキストファイルstudent.txtをからデータを読み込み、テキストファイルの各行をStudentオブジェクトに変換してバイナリファイルstudent.datに書き込み、そのStudentに書き込まれたデータが正しいか確認したいのですが上手くいきません。
どう改善すればよろしいでしょうか?助言くださると助かります。
テキストファイル内の文書は
Sam 001 1999/01/07
Jennifer 002 1998/04/05
Bob 003 1999/03/14
Jonathan 004 1998/09/07
となります。
java
1 2package studenttest; 3import java.io.BufferedReader; 4import java.io.BufferedWriter; 5import java.io.File; 6import java.io.FileInputStream; 7import java.io.FileNotFoundException; 8import java.io.FileOutputStream; 9import java.io.FileReader; 10import java.io.IOException; 11import java.io.InputStreamReader; 12import java.io.OutputStreamWriter; 13import java.io.ObjectInputStream; 14import java.io.ObjectOutputStream; 15import java.util.logging.Level; 16import java.util.logging.Logger; 17public class StudentTest { 18 19 private static void processAllFiles(File dir) { 20 File files[] = dir.listFiles(); 21 for(File file : files) { 22 if(file.isFile()) { 23 if(file.getAbsolutePath().endsWith(".java")) { 24 System.out.println(file.getAbsolutePath()); 25 } 26 } 27 else processAllFiles(file); 28 } 29 } 30 public static void main(String[] args) throws IOException, ClassNotFoundException { 31 32 readStudents("D:\students.txt","big5"); 33 34 } 35 36 37 private static void readObjectStream(String filename) throws ClassNotFoundException, IOException { 38 ObjectInputStream input=null; 39 try { 40 input = new ObjectInputStream(new FileInputStream(filename)); 41 while (true) { 42 Object o = input.readObject(); 43 System.out.println(o); 44 if (o == null) { 45 break; 46 } 47 } 48 input.close(); 49 } catch (IOException | ClassNotFoundException e) { 50 input.close(); 51 } 52 } 53 private static void writeObjectStream(String filename) throws FileNotFoundException, IOException { 54 try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(filename))) { 55 output.writeObject(new Date(1992, 3, 23)); 56 output.writeObject(new Date(1997, 5, 3)); 57 output.writeObject(new Date(2010, 7, 16)); 58 output.writeObject("hello"); 59 output.writeObject(new Student("John","213" , new Date(1997, 5, 3))); 60 } 61 } 62 private static void writeToFileWithEncoding(String fileanme, String encoding) { 63 try { 64 FileOutputStream stream = new FileOutputStream(fileanme); 65 OutputStreamWriter writer = new OutputStreamWriter(stream, encoding); 66 try (BufferedWriter output = new BufferedWriter(writer)) { 67 output.write("Abc"); 68 output.write("オブジェクト指向"); 69 } 70 } catch (IOException ex) { 71 Logger.getLogger(StudentTest.class.getName()).log(Level.SEVERE, null, ex); 72 } 73 } 74 private static void readMembers(String filename, String encoding) throws FileNotFoundException, IOException { 75 try (BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding))) { 76 Student students[] = new Student[3]; 77 int index = 0; 78 while (true) { 79 String line = input.readLine(); 80 if (line == null) { 81 break; 82 } 83 84 String tokens[] = line.split(","); 85 86 String name = tokens[0], id = tokens[1], birthday = tokens[2]; 87 String births[] = birthday.split("/"); 88 89 int yy = Integer.parseInt(births[0]), mm=Integer.parseInt(births[1]),dd=Integer.parseInt(births[2]); 90 91 students[index] = new Student(name, id, new Date(yy, mm, dd)); 92 index++; 93 94 } 95 System.out.println("--------------"); 96 for(Student member : students) System.out.println(member); 97 } 98 } 99 100 private static void readByReaderWithEncoding(String filename, String encoding) throws FileNotFoundException, IOException { 101 FileInputStream stream = new FileInputStream(filename); 102 InputStreamReader reader = new InputStreamReader(stream, encoding); 103 try (BufferedReader input = new BufferedReader(reader)) { 104 while (true) { 105 String line = input.readLine(); 106 if (line == null) { 107 break; 108 } 109 System.out.println(line); 110 } 111 } 112 } 113 private static void readByReaderDefaultEncoding(String filename) throws FileNotFoundException, IOException { 114 try (FileReader reader = new FileReader(filename)) { 115 while (reader.ready()) { 116 117 char[] chIn = new char[10]; 118 119 int nResult = reader.read(chIn); 120 if (nResult == -1) { 121 break; 122 } 123 124 125 System.out.println(chIn); 126 } 127 } 128 } 129 private static void readFileStream(String filename) { 130 try { 131 System.out.println("main started"); 132 try (FileInputStream fis = new FileInputStream(filename)) { 133 System.out.println("input is " + fis); 134 while (fis.available() > 0) { 135 136 byte[] b = new byte[1]; 137 138 int nResult = fis.read(b); 139 if (nResult == -1) { 140 break; 141 } 142 143 System.out.print(new String(b)); 144 } 145 } 146 } catch (FileNotFoundException ex) { 147 Logger.getLogger(StudentTest.class.getName()).log(Level.SEVERE, null, ex); 148 } catch (IOException ex) { 149 Logger.getLogger(StudentTest.class.getName()).log(Level.SEVERE, null, ex); 150 } 151 } 152 private static void readStudents(String dstudentstxt, String big5) { 153 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 154 } 155} 156
java
1 2package studenttest; 3public class Student { 4 String name; 5 String id; 6 Date birthday; 7 public Student(String n,String i, Date d) { 8 name = n; 9 id = i; 10 birthday = d; 11 12 } 13 @Override 14 public String toString() { 15 return name + ": " + id +": " + birthday; 16 } 17} 18
java
1 2package studenttest; 3import java.io.Serializable; 4public class Date implements Serializable { 5 int year, month, day; 6 public Date(int yy, int mm, int dd) { 7 year = yy; 8 month = mm; 9 day = dd; 10 } 11 public String toString() { 12 return String.format("%04d/%02d/%02d", year, month, day); 13 } 14} 15