标签:blog io ar os 使用 java for 文件 on
字符流
FileReader
FileWriter
BufferedReader
BufferedWriter
字节流
InputStream OutputStream
FileInputStream FileOutputStream
BufferedInputStream BufferedOutputStream
import java.io.*;
public class CopyDemo
{
public static void main(String args[]) throws IOException
{
copy_2();
}
public static void copy_1() throws IOException
{
FileWriter fw = new FileWriter("copy.txt");
FileReader fr = new FileReader("litiepeng.txt");
int ch = 0;
while((ch=fr.read())!=-1)
fw.write(ch);
fw.close();
fr.close();
}
public static void copy_2()
{
FileWriter fw = null;
FileReader fr = null;
try
{
fw = new FileWriter("copy2.txt");
fr = new FileReader("litiepeng.txt");
char[] buf = new char[1024]; //这里可能太大了,读的数据没把这里装下
int len = 0;
while ((len=fr.read(buf))!=-1)
{
fw.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("读写失败");
}
finally
{
if (fr!=null)
{
try
{
fr.close();
}
catch (IOException e)
{
}
}
if (fw!=null)
{
try
{
fw.close();
}
catch (IOException e)
{
}
}
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
package pack;
import java.io.*;
public class DemoTwo {
public static void main(String args[]) throws IOException {
FileReader fr = new FileReader("litiepeng.txt");
/*
* 定义一个字符数组,用于存储读到的字符 该read(char [])返回的是读到字符个数 char[] buf = new char[3];
* int num1 = fr.read(buf); sop("num1 = "+new String(buf));
* 这里如果num1返回1的话,想想会怎么样?
* buf里的数据本来是abc 最后读了1个字符d 然后最后的buf是dbc.这时候就不能直接输出buf.只能输出new String(buf,0,num1);
*
* int num2 = fr.read(buf); sop("num2 = "+new
* String(buf,0,num2));//为什么要这样,和上面比较一下就知道了
*/
char[] buf = new char[1024];
int num = 0;
while ((num = fr.read(buf)) != -1) {
sop(new String(buf, 0, num));
}
fr.close();
}
public static void sop(Object obj) {
System.out.print(obj); // 如果打印数据,这里一般用print,不用println因为可以读取一次就换行,但是文件里面不是换行,
}
}
import java.io.*;
public class FileWriterDemo
{
public static void main(String args[])
{
FileWriter fw = null; //这个为什么要放在外面,
try
{
fw = new FileWriter("k:\\demo2.txt");
fw.write("abac");
}
catch (IOException e)
{
sop("catch:"+e.toString());
}
finally
{
try
{
if(fw!=null) //他为什么要这个 因为如果前面抛出异常, fw并没有被创建,但是finally任然要执行. 这里就会有异常.
fw.close();
}
catch (IOException e)
{
sop(e.toString());
}
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
/*
在windows中换行由2个符号表示,\r\n 在Linux里面 就是\n
*/
------------------------
import java.io.*;
public class FileWriter2
{
public static void main(String args[]) throws IOException
{
FileWriter fw = new FileWriter("litiepeng.txt",true);//这个是如果文件存在,在这个文件后面追加.
fw.write("nihao\r\n好不好哦");
fw.close();
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
字符流的抽象基类:
Reader,Writer
*/
import java.io.*;
public class ioDemo
{
public static void main(String args[]) throws IOException
{
//创建一个FileWriter对象,该对象一被初始化就必须要明确被操作的文件
//文件会被创建到指定目录下,如果文件存在,将被覆盖.
// FileWriter fw = new FileWriter("demo.txt",true);
FileWriter fw = new FileWriter("demo.txt");
//调用writer方法,将字符串写入到流中,但是还没存到文件中
fw.write("litiepeng");
//刷新流对象中的缓冲区,将流中的数据写到文件中
fw.flush();
//关闭流资源,但是关闭之前会刷新一次内部的缓冲中的数据.
//和flush区别,flush刷新后,流可以继续使用,close刷新后,会将流关闭.
fw.close();
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
PrintStream
System.setIn();
System.setOut();
package pack;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo
{
public static void main(String args[]) throws IOException
{
try
{
int[] arr = new int[2];
System.out.println(arr[3]);
}
catch (Exception e)
{
try
{
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(d);
PrintStream ps = new PrintStream("exception.log");
ps.println(s); //先打印时间
System.setOut(ps);//这里会有异常产生
}
catch (IOException ee)
{
throw new RuntimeException("日志文件创建失败");
}
e.printStackTrace(System.out);
sop("ddd");
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
标签:blog io ar os 使用 java for 文件 on
原文地址:http://www.cnblogs.com/wikiki/p/4136648.html