Chinaunix首页 | 论坛 | 博客
  • 博客访问: 797700
  • 博文数量: 82
  • 博客积分: 9970
  • 博客等级: 中将
  • 技术积分: 2412
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-25 14:43
文章分类

全部博文(82)

文章存档

2013年(3)

2011年(8)

2010年(9)

2009年(25)

2008年(37)

我的朋友

分类:

2008-03-01 17:01:52

1:文件(file)

新建文件:
File file = new File("D:\\a.txt");

文件常见使用方法:
File.exists()  //文件或者目录是否存在
File.isFile()  //是否是文件
File.isDirectory(); //是否是目录
File.getName(); //取文件或者目录的名字
File.getPath(); //取文件或者目录的路径
File.getAbsolutePath(); //取文件绝对路径
File.lastModified(); //最后修改日期
File.length(); //文件或者目录的子节大小
File.createNewFile(); //创建新文件

2:流(stream)

常用流分为两类:
字节流:8位子节流,基本单位是字节;
字符流:16位Unicode流,基本单位是Unicode字符;

// 输入流(将数据从文件写入内存)
File file = new File("filePath");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
String s;
while ((s = dis.readLine()) != null) {
 System.out.println(s);
}

// 输出流(将内存中的数据写入文件)
File file = new File("filePath");
String Str = new String("Garbage in,我是张三!");
file.createNewFile();
DataOutputStream myStream = new DataOutputStream(new FileOutputStream(file));
myStream.writeChars(Str);

// 输入流
File file = new File("filePath");
BufferedReader br = new BufferedReader(new FileReader(aFile));
String s;
while ((s = dis.readLine()) != null) {
 System.out.println(s);
}
br.close();

// 输出流
String Str = new String("Garbage in,我是张三!");
BufferedWriter bw =  new java.io.BufferedWriter(new FileWriter(file));
bw.write(Str);
bw.flush();
bw.close();

阅读(1933) | 评论(0) | 转发(0) |
0

上一篇:简单web开发

下一篇:socket通信编程

给主人留下些什么吧!~~