File类测试
//根据指定的文件名和路径创建File对象
File file = new File("myFile.txt");
//file.exists();检测文件是否存在
boolean exists = file.exists();
System.out.println(exists);
//按照file对象指定的路径和文件名创建一个新的文件
file.createNewFile();
exists = file.exists();
System.out.println(exists);
System.out.println("Path:"+file.getPath());
//返回文件或目录对应的绝对路径
System.out.println("Abs Path:"+file.getAbsolutePath());
System.out.println("Abs File:"+file.getAbsoluteFile());
/*File newFile = new File("myDir/newFile.txt");
//如果源文件和目标文件在同一个目录下,则将源文件改名为新文件
//如果源文件和目标文件不在同一个目录下,则将源文件移动至新的路径
//文件内容不受影响
file.renameTo(newFile);*/
long lastModified = file.lastModified();
System.out.println(new Date(lastModified));
System.out.println("length:"+file.length());
// canWrite()
System.out.println("W="+file.canWrite());
// canRead()
System.out.println("R="+file.canRead());
// isFile()
System.out.println("F="+file.isFile());
// isDirectory()
System.out.println("D="+file.isDirectory());
File delFile = new File("io.txt");
boolean delete = delFile.delete();
System.out.println("Del="+delete);
InputStream测试
File fileIn = new File("myFile.txt");
//使用InputStream的实现类读取文件内容到内存中
//fileIn指向的文件必须存在
InputStream in = new FileInputStream(fileIn);
//read()读取文件中当前位置的一个字节,如果当前位置已经是文件末尾,则返回-1
/*int read = in.read();
System.out.println((char)read);
read = in.read();
System.out.println(read);*/
//循环读取文件中的全部内容
int read;
while((read = in.read()) != -1){
System.out.print((char)read);
}
//IO流不在垃圾回收机制处理的范围之内,要显式关闭
in.close();
OutputStreamx测试
File fileOut = new File("myFile.txt");
//将内存中的数据写入到文件中
//fileOut如果不存在,则创建一个新文件
OutputStream out = new FileOutputStream(fileOut);
String data = "aaaaaaaaaaaaaaaaaaaaaaaa";
byte[] bytes = data.getBytes();
//将字节信息写入到文件中,但是会覆盖文件之前的内容
out.write(bytes);
Reader测试
File fileRead = new File("myFile.txt");
Reader reader = new FileReader(fileRead);
//从文本文件中读取一个字符
/*int read = reader.read();
System.out.println((char)read);*/
int len;
char [] c = new char[10];
while((len = reader.read(c)) != -1){
System.out.println(new String(c));
}
reader.close();
Writer测试
File fileWrite = new File("fileWriter.txt");
Writer writer = new FileWriter(fileWrite);
String str = "自古逢秋悲寂寥,我言秋日胜春朝,晴空一鹤排云上,便引诗情到碧霄";
writer.write(str.toCharArray());
//flush()方法可以强制将内存中Writer缓冲区的数据写入内存
writer.flush();
writer.close();
转换流测试
FileReader是InputStreamReader的子类
FileWriter是OutputStreamWriter的子类
说明一切文件在计算机中都是以字节形式保存的,所以从根本上来说,读取任何文件都需要使用字节流的方式
为了能够对字符型数据进行有针对性的操作,提高效率,我们使用转换流将数据内容为字符的字节流转换为字符流
File fileSrc = new File("utf-8.txt");
String str = "";
InputStream in = new FileInputStream(fileSrc);//1100010101
InputStreamReader inRead = new InputStreamReader(in,"UTF-8");
int len;
char [] c = new char[1024];
while((len = inRead.read(c)) != -1){
str += new String(c,0,len);
}
System.out.println(str);
inRead.close();
File fileDest = new File("utf-8.txt");
String str = "自古逢秋悲寂寥,我言秋日胜春朝,晴空一鹤排云上,便引诗情到碧霄";
OutputStream out = new FileOutputStream(fileDest);
OutputStreamWriter outWrite = new OutputStreamWriter(out,"UTF-8");
outWrite.write(str);
outWrite.flush();
outWrite.close();
处理流之一:缓冲流:
BufferedInputStream 和 BufferedOutputStream
BufferedReader 和 BufferedWriter
处理流之二:转换流:
转换流提供了在字节流和字符流之间的转换
Java API提供了两个转换流:
InputStreamReader和OutputStreamWriter
字节流中的数据都是字符时,转成字符流操作更高效。
处理流之三:对象流:
ObjectInputStream和OjbectOutputSteam
用于存储和读取基本数据类型数据或对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。
序列化:用ObjectOutputStream类保存基本类型数据或对象的机制
反序列化:用ObjectInputStream类读取基本类型数据或对象的机制
ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量
RandomAccessFile类:
RandomAccessFile 类支持 “随机访问” 的方式,程序可以直接跳到文件的任意地方来读、写文件
支持只访问文件的部分内容
可以向已存在的文件后追加内容
RandomAccessFile 对象包含一个记录指针,用以标示当前读写处的位置。RandomAccessFile 类对象可以自由移动记录指针:
long getFilePointer():获取文件记录指针的当前位置
void seek(long pos):将文件记录指针定位到 pos 位置
构造器
public RandomAccessFile(File file, String mode)
public RandomAccessFile(String name, String mode)
创建 RandomAccessFile 类实例需要指定一个 mode 参数,该参数指定 RandomAccessFile 的访问模式:
r: 以只读方式打开
rw:打开以便读取和写入
rwd:打开以便读取和写入;同步文件内容的更新
rws:打开以便读取和写入;同步文件内容和元数据的更新
Java复习第四天---IO流,布布扣,bubuko.com
原文地址:http://blog.csdn.net/lygscg123/article/details/27374381