Appearance
全局配置
Appearance
input输入 output输出
Java IO操作主要是指使用Java程序完成输入输出操作: 输入:将硬盘中的文件以数据流的形式读取到Java程序中。 输出:用Java程序将数据流写入到硬盘的文件中。
IO流是通过Java程序对文件进行读写的,首先需要明白Java如何来表示文件,Java会将文件抽象成对象,有对象就一定有类,文件对象通过java.io.File类来创建。 File类的实例化对象就是在描述一个物理资源/文件。
public File(String pathname)
// 根据路径创建对象
public String getName()
// 获取文件名
public String getParent()
// 获取文件所在的目录
public File getParentFile()
// 获取文件所在的目录对应的File对象
public String getPath()
// 获取文件路径
public boolean exists()
// 判断对象是否存在
public boolean isDirectory()
// 判断对象是否为目录
public boolean isFile()
// 判断对象是否为文件
public long length()
// 获取文件的大小,以字节为单位
public boolean createNewFile()
// 根据当前对象创建新文件
public boolean delete()
// 删除对象
public boolean mkdir()
// 根据当前对象创建新目录
public boolean renameTo(File dest)
// 为已存在的对象重命名
在UTF-8编码规则下,一个字母,空格,回车,标点符号... 占一个字节 byte一个汉字占3个字节
abc:3
你好:6
createNewFile:如果该file对象不存在,则根据其路径创建该文件,返回true
如果file对象已经存在,则创建失败,返回false
renameTo:给文件重命名,如果原文件和目标文件在不同的路径下,会按照目标文件的路径重新设置该文件的路径,相当于把原文件进行移动。
import java.io.File;
import java.io.IOException;
public class FileTest {
public static void main(String[] args) {
String pathname = "/Users/jiang/Desktop/iotest/test.txt";
File file = new File(pathname);
if(file.exists()) {
String name = file.getName();
System.out.println("文件名:"+name);
String parentPath = file.getParent();
System.out.println("文件所在的目录:"+parentPath);
File parentFile = file.getParentFile();
test(parentFile);
test(file);
System.out.println(file+"的长度:"+file.length());
}
}
}
import java.io.File;
import java.io.IOException;
public class FileTest {
public static void main(String[] args) {
String path = "/Users/southwind/Desktop/iotest/测试.txt";
File file = new File(path);
try {
// System.out.println(file.mkdir());
File file2 = new File("/Users/southwind/Desktop/test.txt");
System.out.println(file.renameTo(file2));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void test(File file) {
boolean flag = file.isDirectory();
if(flag) {
System.out.println(file+"是一个目录");
}else {
System.out.println(file+"不是一个目录");
}
boolean flag2 = file.isFile();
if(flag2) {
System.out.println(file+"是一个文件");
}else {
System.out.println(file+"不是一个文件");
}
}
}
I:input 输入流 O:output 输出流
流就是一组有序的数据序列,以先进先出的方式发送数据的通道。
Java中的流有很多种不同的分类: 1.按照方向分,可以分为输入流和输出流,如何判断输入还是输出,站在Java程序角度,看数据是从外界来到Java中呢还是从Java中输出到外界,外届-Java 就是输入流,Java-外界 就是输出流。 2.按照单位分,可以分为字节流和字符流,字节流指每次处理数据都是以字节为单位,字符流是指每次处理数据都是以字符为单位。 字节流按照方向可以分为输入字节流(InputStream)和输出字节流(OutputStream) 3.按照功能分,可以分为节点流和处理流。
输入字节流 InputStream,通过该流可以将文件中的数据读入到Java程序中。 InputStream是java.io包中的顶层父类,实现了Closeable接口,该接口的作用是每次操作结束之后释放资源。
public abstract int read()
// 以字节为单位读取文件中的数据。read()读取数据,每一此read()返回值就是一个字节的数据,如果取出的数据为-1,则表示该文件的所有的数据已经全部取出。
public int read(byte b[])
// 将数据存入byte类型的数组中,并返回数据的长度
public int read(byte b[],int off,int len)
// 将数据存入byte类型的数组的指定区间中,并返回数据的长度
public byte[] readAllBytes()
// 将数据存入byte类型的数组中,并返回该数组
public int available()
// 返回当前数据中未读取的数据个数
public void close()
// 关闭数据流
实际开发中不能直接实例化InputStream,因为它是一个抽象类,应该实例化其实现了抽象方法的子类,FileInputStream。
FileInputStream的实例化就相当于一根连接到某个文件的管道,通过该管道就可以把文件中的数据取出读取到Java程序中,FileInputStream只能连接文件,不能连接目录,否则会抛出FileNotFoundException。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IOTest {
public static void main(String[] args) {
File file = new File("/Users/southwind/Desktop/test.txt");
try {
if (file.isFile()) {
InputStream inputStream = new FileInputStream(file);
System.out.println(inputStream);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IOTest {
public static void main(String[] args) {
try {
InputStream inputStream = new FileInputStream("/Users/southwind/Desktop/test.txt");
System.out.println(inputStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IOTest {
public static void main(String[] args) {
try {
File file = new File("/Users/southwind/Desktop/test.txt");
InputStream inputStream = new FileInputStream(file);
long length = file.length();
for(int i = 0; i < length; i++) {
int temp = inputStream.read();
System.out.println(temp);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IOTest {
public static void main(String[] args) {
try {
int temp = inputStream.read();
System.out.println(temp);
while(temp != -1) {
temp = inputStream.read();
if(temp == -1) {
break;
}
System.out.println(temp);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IOTest {
public static void main(String[] args) {
try {
System.out.println("当前还有"+inputStream.available()+"byte的数据未读");
int temp = 0;
while((temp = inputStream.read())!=-1) {
System.out.println("当前还有"+inputStream.available()+"byte的数据未读");
System.out.println(temp);
}
inputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IOTest {
public static void main(String[] args) {
try {
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IOTest {
public static void main(String[] args) {
try {
byte[] bytes = new byte[2];
int length = inputStream.read(bytes);
System.out.println(length);
for (byte b : bytes) {
System.out.println(b);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IOTest {
public static void main(String[] args) {
try {
byte[] bytes = new byte[10];
int length = inputStream.read(bytes,3,2);
System.out.println(length);
for (byte b : bytes) {
System.out.println(b);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IOTest {
public static void main(String[] args) {
try {
InputStream inputStream = new FileInputStream("/Users/southwind/Desktop/test.txt");
byte[] bytes = inputStream.readAllBytes();
for (byte b : bytes) {
System.out.println(b);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
OutputStream跟InputStream类似,也是一个抽象父类,
public abstract void write(int b)
// 以字节为单位写数据
public void write(byte b[])
// 将byte类型数组中的数据写出
public void write(byte b[],int off,int len)
// 将byte类型数组中指定区间的数据写出
public void flush()
// 可以强制将缓冲区的数据同步到输出流中
public void close()
// 关闭数据流
实际开发中不能直接实例化OutputStream的对象,因为它是一个抽象类,我们应该实例化其实现了抽象方法的字了FileOutputStream。
FileOutputStream(File file) file如果不存在,则自动创建该文件。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class OutputStreamTest {
public static void main(String[] args) {
try {
OutputStream outputStream = new FileOutputStream("/Users/southwind/Desktop/test.txt");
outputStream.write(99); // c
outputStream.write(111); // o
byte[] bytes = {97,98,99};
outputStream.write(bytes); // abc
outputStream.write(bytes, 0, 1); // a
byte[] bytes1 = {(byte)228,(byte)189,(byte)160,(byte)229,(byte)165,(byte)189};
outputStream.write(bytes1); // 你好
outputStream.write(bytes1, 3, 3); // 好
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
字符流是有别于字节流的另外一种数据流,两者的区别在于每次处理数据的单位不同,字节流是以字节为单位,字符流是以字符为单位。
字符流按照方向可以分为输入字符流(Reader)和输出字符流(Writer)
Reader是一个抽象类,实现了Readable和Colseable两个接口,Closeable接口表示该类的实例化对象可以手动释放资源,Readable接口的作用是可以将字符读入指定的字符缓冲区。
public int read()
// 以字符为单位读取数据
public int read(char cbuf[])
// 将数据读入char类型的数组,并返回数据长度
public int read(char cbuf[],int off,int len)
// 将数据读入char类型数组的指定区间,并返回数据长度
public abstarct void close()
// 关闭数据流
public long transferTo(Writer out)
// 将数据直接读入字符输出流
我们在使用Reader进行读入操作时,不能直接实例化Reader的对象,因为Reader是一个抽象类,我们应该实例化其实现了抽象方法的子类FileReader。
在UTF-8编码规范下,一个英文字母对应一个字节(byte),对应一个字符(char)
一个汉字对应三个字节(byte),对应一个字符(char)。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
public class PngTest {
public static void main(String[] args) {
try {
// 字节流读取
InputStream inputStream = new FileInputStream("/Users/southwind/Desktop/1.png");
int temp = 0;
OutputStream outputStream = new FileOutputStream("/Users/southwind/Desktop/2.png");
while((temp = inputStream.read())!=-1) {
outputStream.write(temp);
}
inputStream.close();
outputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
public class PngTest {
public static void main(String[] args) {
try {
// 字符流读取
Reader reader = new FileReader("/Users/southwind/Desktop/1.png");
int temp = 0;
Writer writer = new FileWriter("/Users/southwind/Desktop/2.png");
while((temp = reader.read())!=-1) {
writer.write(temp);
}
reader.close();
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
用IO进行文件的读写时,输入流和输出流的类型必须一致,即要么都是字节流,要么都是字符流,不能用字节流读取,然后用字符流输出,也不能用字符流然后用字节流输出,否则会导致文件的数据错误,因为打乱了数据的内部排列结构。
如果是文本类型的数据,txt,word既可以用字节流完成读取,也可以用字符流完成读取。
但是非文本类型的,如图片,视频,音频等,只能用字节流读取,用字符流读取同样会导致文件的数据错误,从而导致文件无法打开。
Writer输出字符流,和Reader一样,它也是一个抽象类。 Appendable接口的实现类的对象能够添加char序列和值。
public void write(int c)
// 以字符为单位写数据
public void write(char cbuf[])
// 将插入类型数组中的数据写出
public abstract void write(char cbuf[],int off,int len)
// 将插入类型数组中指定区间的数据写出
public void write(String str)
// 将String类型的数据写出
public void write(String str,int off,int len)
// 将String类型指定区间的数据写出
public abstract void flush()
// 可以强制将缓冲区的数据同步到输出流中
public abstract void close()
// 关闭数据流
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class WriterTest {
public static void main(String[] args) {
try {
Writer writer = new FileWriter("/Users/southwind/Desktop/test.txt");
writer.write(20320); // 你
writer.write(22909); // 好
char[] chars = {'你','好','世','界'};
writer.write(chars);
String str = "Hello World";
writer.write(chars,2,2);
writer.write(str);
writer.write(str, 6, 3);
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
IO流按照功能可以分为节点流和处理流: 节点流指基础流(字节流和字符流); 处理流指建立在节点流的基础上,对节点流进行包装的流(字节字符转换流,缓冲流等)。
如何区分节点流和处理流? 看该流能否直接作用于File对象,如果能直接关联到File对象就是节点流,否则就是处理流。
如何判断流能否直接作用于File对象呢? 看该流的构造函数,是否提供了参数为File对象或者文件路径的构造函数,如果提供了,则表示可以直接作用于FIle,则该流就是节点流,否则就是处理流。
如何将字节流转为字符流,Java提供了完成转换的类,按照方向可以分为输入转换流(InputStreamReader)和输出转换流(OutputStreamWriter)。
文件 <--> 输入字节流(inputStream) <--> 输入处理流(inputStreamReader) <--> 输入字符流(Reader) <--> 程序 <--> 输出字节流(outputStream) <--> 输出处理流(outputStreamWriter) <--> 输出字符流(Writer) <--> 文件
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class Test {
public static void main(String[] args) {
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
Reader reader = null;
try {
// 节点流:输入字节流
inputStream = new FileInputStream("/Users/southwind/Desktop/test.txt");
// 处理流:输入处理流
inputStreamReader = new InputStreamReader(inputStream);
// 在处理流这一步已经可以当做字符流来处理数据
reader = inputStreamReader;
int temp = 0;
while((temp = reader.read())!=-1) {
System.out.println(temp);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
reader.close();
inputStreamReader.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class Test2 {
public static void main(String[] args) {
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
Writer writer = null;
try {
outputStream = new FileOutputStream("/Users/southwind/Desktop/test2.txt");
outputStreamWriter = new OutputStreamWriter(outputStream);
writer = outputStreamWriter;
String str = "111";
char[] chars = str.toCharArray();
writer.write(chars);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
writer.close();
outputStreamWriter.close();
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
缓冲流可以分为字节缓冲流、字符缓冲流。 字节缓冲流可分为字节输入缓冲流,字节输出缓冲流; 字符缓冲流可以分为字符输入缓冲流,字符输出缓冲流。
BufferedInputStream 字节输入缓冲流,使用字节流作为基础管道的基础上,提供了一个缓冲区,在访问硬盘时缓冲区可以一次性存入多个字节,从缓冲区中将数据读入到字节流中,再到程序内存中。
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class Test3 {
public static void main(String[] args) {
InputStream inputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
inputStream = new FileInputStream("/Users/southwind/Desktop/test.txt");
bufferedInputStream = new BufferedInputStream(inputStream);
int temp = 0;
while((temp = bufferedInputStream.read())!=-1) {
System.out.println(temp);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bufferedInputStream.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class Test3 {
public static void main(String[] args) {
InputStream inputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
inputStream = new FileInputStream("/Users/southwind/Desktop/test.txt");
bufferedInputStream = new BufferedInputStream(inputStream);
int temp = 0;
byte[] bytes = new byte[30];
int length = bufferedInputStream.read(bytes);
System.out.println(length);
for (byte b : bytes) {
System.out.println(b);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bufferedInputStream.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class Test3 {
public static void main(String[] args) {
InputStream inputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
inputStream = new FileInputStream("/Users/southwind/Desktop/test.txt");
bufferedInputStream = new BufferedInputStream(inputStream);
int temp = 0;
byte[] bytes = new byte[30];
int length = bufferedInputStream.read(bytes,3,6);
System.out.println(length);
for (byte b : bytes) {
System.out.println(b);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bufferedInputStream.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
BufferedReader 字符输入缓冲流,readLine()方法可以直接读取一整行数据。
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class Test4 {
public static void main(String[] args) {
InputStream inputStream = null;
Reader reader = null;
BufferedReader bufferedReader = null;
try {
inputStream = new FileInputStream("/Users/southwind/Desktop/test.txt");
reader = new InputStreamReader(inputStream);
// Reader reader = new FileReader("/Users/southwind/Desktop/test.txt");
// 需要传入字符流
bufferedReader = new BufferedReader(reader);
String str = null;
// 使用readLine()
while((str = bufferedReader.readLine())!=null) {
System.out.println(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bufferedReader.close();
reader.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class Test4 {
public static void main(String[] args) {
InputStream inputStream = null;
Reader reader = null;
BufferedReader bufferedReader = null;
try {
inputStream = new FileInputStream("/Users/southwind/Desktop/test.txt");
reader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(reader);
int temp = 0;
// 使用read(),读取字节
while((temp = bufferedReader.read())!=-1) {
System.out.println(temp);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bufferedReader.close();
reader.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class Test4 {
public static void main(String[] args) {
InputStream inputStream = null;
Reader reader = null;
BufferedReader bufferedReader = null;
try {
inputStream = new FileInputStream("/Users/southwind/Desktop/test.txt");
reader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(reader);
char[] chars = new char[20];
// read()方法传入char类型数组
int length = bufferedReader.read(chars);
System.out.println(length);
for (char c : chars) {
System.out.println(c);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bufferedReader.close();
reader.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class Test4 {
public static void main(String[] args) {
InputStream inputStream = null;
Reader reader = null;
BufferedReader bufferedReader = null;
try {
inputStream = new FileInputStream("/Users/southwind/Desktop/test.txt");
reader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(reader);
char[] chars = new char[100];
// read()方法传入char类型数组,并指定范围
int length = bufferedReader.read(chars,30,30);
System.out.println(length);
for (char c : chars) {
System.out.println(c);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bufferedReader.close();
reader.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
BufferedOutputStream 字节输出缓冲流
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Test5 {
public static void main(String[] args) {
OutputStream outputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
outputStream = new FileOutputStream("/Users/southwind/Desktop/test2.txt");
bufferedOutputStream = new BufferedOutputStream(outputStream);
bufferedOutputStream.write(22309);
bufferedOutputStream.write(22310);
bufferedOutputStream.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bufferedOutputStream.close();
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Test5 {
public static void main(String[] args) {
OutputStream outputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
outputStream = new FileOutputStream("/Users/southwind/Desktop/test2.txt");
bufferedOutputStream = new BufferedOutputStream(outputStream);
String str = "BufferedInputStream 字节输入缓冲流,使用字节流作为基础管道的基础上,提供了一个缓冲区,在访问硬盘时缓冲区可以一次性存入多个字节,从缓冲区中将数据读入到字节流中,再到程序内存中。";
byte[] bytes = str.getBytes();
// 传入bytes类型数组
bufferedOutputStream.write(bytes);
bufferedOutputStream.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bufferedOutputStream.close();
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Test5 {
public static void main(String[] args) {
OutputStream outputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
outputStream = new FileOutputStream("/Users/southwind/Desktop/test2.txt");
bufferedOutputStream = new BufferedOutputStream(outputStream);
String str = "字节输入缓冲流,使用字节流作为基础管道的基础上,提供了一个缓冲区,在访问硬盘时缓冲区可以一次性存入多个字节,从缓冲区中将数据读入到字节流中,再到程序内存中。";
byte[] bytes = str.getBytes();
// 传入bytes类型数组,并指定范围
bufferedOutputStream.write(bytes,3,21);
bufferedOutputStream.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bufferedOutputStream.close();
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
BufferedWriter 字符输出缓冲流
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Test6 {
public static void main(String[] args) {
Writer writer = null;
BufferedWriter bufferedWriter = null;
try {
writer = new FileWriter("/Users/southwind/Desktop/test2.txt");
bufferedWriter = new BufferedWriter(writer);
String str = "字节输入缓冲流,使用字节流作为基础管道的基础上,提供了一个缓冲区,在访问硬盘时缓冲区可以一次性存入多个字节,从缓冲区中将数据读入到字节流中,再到程序内存中。";
// 直接使用string
bufferedWriter.write(str);
bufferedWriter.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bufferedWriter.close();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Test6 {
public static void main(String[] args) {
Writer writer = null;
BufferedWriter bufferedWriter = null;
try {
writer = new FileWriter("/Users/southwind/Desktop/test2.txt");
bufferedWriter = new BufferedWriter(writer);
String str = "字节输入缓冲流,使用字节流作为基础管道的基础上,提供了一个缓冲区,在访问硬盘时缓冲区可以一次性存入多个字节,从缓冲区中将数据读入到字节流中,再到程序内存中。";
// 使用int类型编码
bufferedWriter.write(22039);
bufferedWriter.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bufferedWriter.close();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Test6 {
public static void main(String[] args) {
Writer writer = null;
BufferedWriter bufferedWriter = null;
try {
writer = new FileWriter("/Users/southwind/Desktop/test2.txt");
bufferedWriter = new BufferedWriter(writer);
String str = "字节输入缓冲流,使用字节流作为基础管道的基础上,提供了一个缓冲区,在访问硬盘时缓冲区可以一次性存入多个字节,从缓冲区中将数据读入到字节流中,再到程序内存中。";
char[] chars = str.toCharArray();
// 使用char类型数组
bufferedWriter.write(chars);
bufferedWriter.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bufferedWriter.close();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Test6 {
public static void main(String[] args) {
Writer writer = null;
BufferedWriter bufferedWriter = null;
try {
writer = new FileWriter("/Users/southwind/Desktop/test2.txt");
bufferedWriter = new BufferedWriter(writer);
String str = "字节输入缓冲流,使用字节流作为基础管道的基础上,提供了一个缓冲区,在访问硬盘时缓冲区可以一次性存入多个字节,从缓冲区中将数据读入到字节流中,再到程序内存中。";
char[] chars = str.toCharArray();
// 使用char类型数组指定范围
bufferedWriter.write(chars,5,10);
bufferedWriter.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bufferedWriter.close();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Test6 {
public static void main(String[] args) {
Writer writer = null;
BufferedWriter bufferedWriter = null;
try {
writer = new FileWriter("/Users/southwind/Desktop/test2.txt");
bufferedWriter = new BufferedWriter(writer);
String str = "字节输入缓冲流,使用字节流作为基础管道的基础上,提供了一个缓冲区,在访问硬盘时缓冲区可以一次性存入多个字节,从缓冲区中将数据读入到字节流中,再到程序内存中。";
// 使用string,指定范围
bufferedWriter.write(str, 5, 15);
bufferedWriter.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bufferedWriter.close();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
序列化是指将内存中的对象输出到硬盘文件中进行保存,反序列化就是相反的操作,从文件中读取数据并还原成内存中的对象。
序列化和反序列化操作: 1.让类实现序列化接口(java.io.Serializable) 2.通过数据流完成对象到文件的读写
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import com.southwind.entity.Student;
public class Test {
public static void main(String[] args) {
//1.创建对象
Student student = new Student();
student.setId(1);
student.setName("张三");
//2.通过数据流将对象写入到硬盘的文件中
OutputStream outputStream = null;
ObjectOutputStream objectOutputStream = null;
try {
outputStream = new FileOutputStream("/Users/southwind/Desktop/student.txt");
// 处理流:对象字节输出流
objectOutputStream = new ObjectOutputStream(outputStream);
// 调用writeObject()输出
objectOutputStream.writeObject(student);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
objectOutputStream.flush();
objectOutputStream.close();
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.Serializable;
// 实现Serializable接口
public class Student extends People implements Serializable,Comparable<Student>{
private int id;
private String name;
public void getId(int id) {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
return 0;
}
}
public class People {
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import com.southwind.entity.Student;
public class Test2 {
public static void main(String[] args) {
InputStream inputStream = null;
ObjectInputStream objectInputStream = null;
try {
inputStream = new FileInputStream("/Users/southwind/Desktop/student.txt");
// 对象字节输入流
objectInputStream = new ObjectInputStream(inputStream);
// 调用readObject()方法
Student student = (Student) objectInputStream.readObject();
System.out.println(student.getId());
System.out.println(student.getName());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
objectInputStream.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}