写文件
-----------------------------------------------------------------------
try {
DataOutputStream out2 =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("Data.txt")));
out2.writeDouble(3.14159);
out2.writeChars("That was pi\n");
out2.writeBytes("That was pi\n");
out2.close();
DataInputStream in5 =
new DataInputStream(
new BufferedInputStream(
new FileInputStream("Data.txt")));
BufferedReader in5br =
new BufferedReader(
new InputStreamReader(in5));
// Must use DataInputStream for data:
System.out.println(in5.readDouble());
// Can now use the "proper" readLine():
System.out.println(in5br.readLine());
// But the line comes out funny.
// The one created with writeBytes is OK:
System.out.println(in5br.readLine());
} catch(EOFException e) {
System.err.println("End of stream");
}
-----------------------------------------------------------------------
try {
DataOutputStream out2 =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("Data.txt")));
out2.writeDouble(3.14159);
out2.writeChars("That was pi\n");
out2.writeBytes("That was pi\n");
out2.close();
DataInputStream in5 =
new DataInputStream(
new BufferedInputStream(
new FileInputStream("Data.txt")));
BufferedReader in5br =
new BufferedReader(
new InputStreamReader(in5));
// Must use DataInputStream for data:
System.out.println(in5.readDouble());
// Can now use the "proper" readLine():
System.out.println(in5br.readLine());
// But the line comes out funny.
// The one created with writeBytes is OK:
System.out.println(in5br.readLine());
} catch(EOFException e) {
System.err.println("End of stream");
}
----------------------------------------------------------
BufferedReader in =
new BufferedReader(
new FileReader("IOStreamDemo.java"));
String s, s2 = new String();
while((s = in.readLine())!= null)
s2 += s + "\n";
in.close();
System.out.println("===========开始记录日志信息============");
String newLine = null;
String filePath = this.sysInfo.logDir + "log/logActionIDLog.log";
//建立输入流
File logFile = new File(filePath);
boolean exits = logFile.createNewFile();
System.out.println("=========文件已经存在==========" + !exits);
FileWriter fWriter = new FileWriter(logFile, true);
PrintWriter out = new PrintWriter(fWriter, true); ;
if (exits) {
newLine =
"first";
//写记录,并关闭流
out.println(newLine);
//out.close();
//fWriter.close();
}
//组成记录
newLine = "test" //写记录,并关闭流
out.println(newLine);
out.close();
fWriter.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException ie) {
ie.printStackTrace();
}
System.out.println("========结束记录日志信息=========");