Java IO
一 概述
通过数据流、序列化和文件系统提供系统输入和输出,即Java 输入输出系统。
流
在java IO中,流是一个核心的概念。流即数据流,你可以从中读到数据,你也可以往里面写入数据。流是数据流向的媒介。在java中,可以是字符流,也可以是字节流。
二 Java IO类库
1 Java IO类型
Java IO可以分成输入流(inputStream/reader)和输出流(outputstream/writer)从处理流的类型上看,java IO又可以分为字符流(reader和writer)和字节流(inputstream和outputstream)。
我们编写的程序通过inputStream/reader从数据源读入,然后把数据用outputstream/writer写入目标媒介中去。其中inputStream/reader数据源相关联outputstream/writer与目标媒介相关联。
2 IO类库
在我们实际的应用中我们不止用到了以上的四个类,更多的是用到了它的子类,由于每一个子类负责各自的,所以可以方便我们的使用。其用途包括:文件访问、网络访问、内存缓存访问、缓冲、读写文本 、读写基本类型数据 、读写对象等。
java io 类的集成关系:
三 javaIO的用法
3.1字节流
字节流对应的类是InputStream和OutputStream,但在实际的开发中我们用到他的子类比较多,下面给出两个例子:
用字节流写文件
public static void writeToFile() throws IOException{
String hello= new String( "hello word!");
byte[] byteArray= hello.getBytes();
File file= new File( "d:/test.txt");
//因为是用字节流来写媒介,所以对应的是OutputStream
//又因为媒介对象是文件,所以用到子类是FileOutputStream
OutputStream os= new FileOutputStream( file);
os.write( byteArray);
os.close();
}
用字节流读文件
public static void readFromFile() throws IOException{
File file= new File( "d:/test.txt");
byte[] byteArray= new byte[( int) file.length()];
//因为是用字节流来读媒介,所以对应的是InputStream
//又因为媒介对象是文件,所以用到子类是FileInputStream
InputStream is= new FileInputStream( file);
int size= is.read( byteArray);
System. out.println( "大小:"+size +";内容:" +new String(byteArray));
is.close();
}
3.2字符流
字符流对应的类是Reader和Writer。下面我们就用字符流来操作文件媒介:
用字符流写文件:
public static void writeCharToFile() throws IOException{
String hello= new String( "hello word!");
File file= new File( "d:/test.txt");
//因为是用字符流来读媒介,所以对应的是Writer,又因为媒介对象是文件,所以用到子类是FileWriter
Writer os= new FileWriter( file);
os.write( hello);
os.close();
}
用字符流读文件:
public static void readCharFromFile() throws IOException{
File file= new File( "d:/test.txt");
//因为是用字符流来读媒介,所以对应的是Reader
//又因为媒介对象是文件,所以用到子类是FileReader
Reader reader= new FileReader( file);
char [] byteArray= new char[( int) file.length()];
int size= reader.read( byteArray);
System. out.println( "大小:"+size +";内容:" +new String(byteArray));
reader.close();
}
3.3字节流转换为字符流
字节流可以转换成字符流,在java.io包中提供的InputStreamReader类就可以实现,当然从其命名上就可以看出它的作用。其实这涉及到另一个概念,IO流的组合,后面我们详细介绍。下面看一个简单的例子:
public static void convertByteToChar() throws IOException{
File file= new File( "d:/test.txt");
//获得一个字节流
InputStream is= new FileInputStream( file);
//把字节流转换为字符流,其实就是把字符流和字节流组合的结果。
Reader reader= new InputStreamReader( is);
char [] byteArray= new char[( int) file.length()];
int size= reader.read( byteArray);
System. out.println( "大小:"+size +";内容:" +new String(byteArray));
is.close();
reader.close();
}
3.4文件媒介操作
3.4.1 File媒介
public class FileDemo {
public static void main(String[] args) {
//检查文件是否存在
File file = new File( "d:/test.txt");
boolean fileExists = file.exists();
System. out.println( fileExists);
//创建文件目录,若父目录不存在则返回false
File file2 = new File( "d:/fatherDir/subDir");
boolean dirCreated = file2.mkdir();
System. out.println( dirCreated);
//创建文件目录,若父目录不存则连同父目录一起创建
File file3 = new File( "d:/fatherDir/subDir2");
boolean dirCreated2 = file3.mkdirs();
System. out.println( dirCreated2);
File file4= new File( "d:/test.txt");
//判断长度
long length = file4.length();
//重命名文件
boolean isRenamed = file4.renameTo( new File("d:/test2.txt"));
//删除文件
boolean isDeleted = file4.delete();
File file5= new File( "d:/fatherDir/subDir");
//是否是目录
boolean isDirectory = file5.isDirectory();
//列出文件名
String[] fileNames = file5.list();
//列出目录
File[] files = file4.listFiles();
}
3.4.2随机读取File文件
在上面的例子我们可以用FileInputStream或者FileReader读文件,但是这两种有一个缺陷,这两种方式只能从头读到尾。但是在实际使用中我们看你需要重文件中开始读,我们可以利用RandomAccessFile的seek()方法,用来定位将要读写文件的指针位置。下面通过一个例子来学习:
随机读入文件
public static void randomAccessFileRead() throws IOException {
// 创建一个RandomAccessFile对象
RandomAccessFile file = new RandomAccessFile( "d:/test.txt", "rw");
// 通过seek方法来移动读写位置的指针
file.seek(10);
// 获取当前指针
long pointerBegin = file.getFilePointer();
// 从当前指针开始读
byte[] contents = new byte[1024];
file.read( contents);
long pointerEnd = file.getFilePointer();
System. out.println( "pointerBegin:" + pointerBegin + "\n" + "pointerEnd:" + pointerEnd + "\n" + new String(contents));
file.close();
}
随机写入文件
public static void randomAccessFileWrite() throws IOException {
// 创建一个RandomAccessFile对象
RandomAccessFile file = new RandomAccessFile( "d:/test.txt", "rw");
// 通过seek方法来移动读写位置的指针
file.seek(10);
// 获取当前指针
long pointerBegin = file.getFilePointer();
// 从当前指针位置开始写
file.write( "HELLO WORD".getBytes());
long pointerEnd = file.getFilePointer();
System. out.println( "pointerBegin:" + pointerBegin + "\n" + "pointerEnd:" + pointerEnd + "\n" );
file.close();
}
3.5 BufferedReader和BufferedWriter
BufferedReader、BufferedWriter 的作用基本和BufferedInputStream、BufferedOutputStream一致,具体用法和原理都差不多 ,只不过一个是面向字符流一个是面向字节流,下面给出一个具体的使用例子:
public static void readByBufferedReader() throws IOException {
File file = new File( "d:/test.txt");
// 在字符流基础上用buffer流包装,也可以指定buffer的大小
Reader reader = new BufferedReader( new FileReader(file),2*1024);
char[] byteArray = new char[( int) file.length()];
int size = reader.read( byteArray);
System. out.println( "大小:" + size + ";内容:" + new String(byteArray));
reader.close();
}
至此,本人学习io暂告一段落,希望能够帮助大家。