Um mit DataOuputStream zu schreiben:
DataOutputStream out = new DataOutputStream(
new FileOutputStream("file.txt"));
out.writeDouble(10.8);
out.writeChar('\t');
out.writeInt(5);
out.writeChars(" \t ok \n");
out.close();
mit Datainputstream zu lesen:
DataInputStream in = new DataInputStream(
new FileInputStream("file.txt"));
try {
while (true) {
price = in.readDouble();
in.readChar(); //throws out the tab
unit = in.readInt();
in.readChar(); //throws out the tab
status= new StringBuffer(20);
char lineSep = System.getProperty("line.separator").charAt(0);
char chr;
while ((chr = in.readChar() != lineSep) {
desc.append(chr);
}
System.out.println("You've ordered " + unit + " units of "
+ desc + " at $" + price);
total = total + unit * price;
}
} catch (EOFException e) { }
System.out.println("For a TOTAL of: $" + total);
in.close();
nutzen zu können, Filewriter:
String name = "John Doe";
int age = 44;
double temp = 26.9;
FileWriter fw;
try {
fw = new FileWriter(new File("mytextfile.txt"));
fw.write(String.format("My name is %s.",name));
fw.write(System.lineSeparator()); //new line
fw.write(String.format("I am %d years old.",age));
fw.write(System.lineSeparator()); //new line
fw.write(String.format("Today's temperature is %.2f.",temp));
fw.write(System.lineSeparator()); //new line
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
Filereader verwenden:
FileReader fr = new FileReader("FileReaderDemo.java");
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null) {
System.out.println(s); // or you can use the wrapper classes
}
fr.close();
Bitte formatieren Sie den Code in Ihrer Frage richtig, indem Sie ihn mit 4 Leerzeichen einrücken. – sidneydobber
hey Sie schreiben einen String in Ihre Datei und Doppel lesen –