java流操作

来源:互联网 发布:云免数据库怎么登陆 编辑:程序博客网 时间:2024/04/30 02:30
 

 try{}

catch(IOException e)

{

]

e.getMessage()方法可用于找出对象的详细信息

e.getClass().getName()能够得到异常对象的实际类型

 

FileWriter out=new FileWriter("output.txt");

等价于 OutputStreamWriter out=new OutputStringWriter(new FIleoutputStream("output.txt"));

 

PrintWriter out=new PrintWriter(new FileWriter("employee.txt"));

等价于PrintWriter out=new PrinWriter(new FileOutputStream("employee.txt"));

 

String name="Harry Hacker";

double salary=7500;

out.print(name);

out.print(' ');

out.print(salary);

上面的代码会把 Harry Hacker 7500写到out中,这些字符接着被转化为字节最终显示到employee.txt中。

 

BufferedReader in=new BufferedReader(new FileReader("employee.txt"));

String line;

while((line=in.readline())!=null)

{

  do something with the line

}

 

BufferedReader in2=new BufferedReader(new InputStreamReader(url.openStream()));

 

存储可变类型对象

ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("employee.txt"));

Employee harry=new Employee("Harry",5000,1989,10,1);

out.writeObject(harry);

ObjectInputStream in =new ObjectInputStream(new FileInputStream(employee.txt));

Employee e1=(Employee)in.readObject();

 

 

File f=new File("test.txt");

会在当前目录下的test.txt文件创建一个File对象,如果提供的文件名没有对应的文件,那么该调用不会创建一个文件。实际上,从一个File对象创建的的一个文件可以通过几个流类的构造器完成,或者使用FILE类的createNewFile方法。

利用File可以创建目录

 

流操作主要需要掌握,字节,字符,字串之间的转化关系。

原创粉丝点击