Java I/O流如何使用
导读:本文共5461字符,通常情况下阅读需要18分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要:");}catch(IOExceptione){thrownewRuntimeException(e);}}第二种方式:/创建文件,第二种方式/@TestpublicvoidcreateFile02(){FileparentFile=newFile("D:\");StringfileName="news.txt";Filefi... ...
目录
(为您整理了一些要点),点击可以直达。");
}catch(IOExceptione){
thrownewRuntimeException(e);
}
}
第二种方式:
/
创建文件,第二种方式
/
@Test
publicvoidcreateFile02(){
FileparentFile=newFile("D:\");
StringfileName="news.txt";
Filefile=newFile(parentFile,fileName);
try{
file.createNewFile();
System.out.println("文件创建成功!");
}catch(IOExceptione){
thrownewRuntimeException(e);
}
}
第三种方式:
/
创建文件,第三种方式
/
@Test
publicvoidcreateFile03(){
StringparentPath="D:\";
StringfileName="my.txt";
Filefile=newFile(parentPath,fileName);
try{
file.createNewFile();
System.out.println("文件创建成功!");
}catch(IOExceptione){
thrownewRuntimeException(e);
}
}
3.获取文件信息
示例代码:
/
获取文件信息
/
@Test
publicvoidinfo(){
//创建文件对象
Filefile=newFile("D:\news.txt");
//获取文件名字
System.out.println(file.getName());
//获取文件路径
System.out.println(file.getAbsolutePath());
//获取文件父亲目录
System.out.println(file.getParent());
//获取文件大小(字节)
System.out.println(file.length());
//文件是否存在
System.out.println(file.exists());
//是不是一个文件
System.out.println(file.isFile());
//是不是一个目录
System.out.println(file.isDirectory());
}
4.目录操作
案例一:判断指定目标是否存在,如果存在就删除
//判断指定目标是否存在,如果存在就删除
StringfilePath="D:\news.txt";
Filefile=newFile(filePath);
if(file.exists()){
if(file.delete()){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
}else{
System.out.println("文件不存在!");
}
案例二:判断目录是否存在,如果存在即删除
//判断目录是否存在,如果存在即删除
StringdirPath="D:\demo";
Filefile1=newFile(dirPath);
if(file1.exists()){
if(file1.delete()){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
}else{
System.out.println("目录不存在!");
}
案例三:判断指定目录是否存在,不存在就创建目录
//判断指定目录是否存在,不存在就创建目录
Stringpersondir="D:\demo\a\b\c";
Filefile2=newFile(persondir);
if(file2.exists()){
System.out.println("该目录存在!");
}else{
if(file2.mkdirs()){
System.out.println("目录创建成功!");
}else{
System.out.println("目录创建失败!");
}
}
注意:创建多级目录,使用mkdirs,创建一级目录使用mkdir
5.字节输入流InputStream
InputStream抽象类是所有类字节输入流的超类
FileInputStream 文件输入流
BufferedInputStream 缓冲字节输入流
ObjectInputStream 对象字节输入流
FileInputStream
文件输入流,从文件中读取数据,示例代码:
单个字节的读取,效率较低:
/
read()读文件
/
@Test
publicvoidreadFile01()throwsIOException{
StringfilePath="D:\hacker.txt";
intreadData=0;
FileInputStreamfileInputStream=null;
try{
fileInputStream=newFileInputStream(filePath);
//读文件,一个字符一个字符读取,返回字符的ASCII码,文件尾部返回-1
while((readData=fileInputStream.read())!=-1){
System.out.print((char)readData);
}
}catch(IOExceptione){
thrownewRuntimeException(e);
}finally{
//关闭流资源
fileInputStream.close();
}
}
注意:使用字节流的方式,无法读取文件中的中文字符,如需读取中文字符,最好使用字符流的方式
使用byte数组的方式读取,这使得可以读取中文,并且有效的提升读取效率:
/*
read(byte[]b)读文件
支持多字节读取,提升效率
/
@Test
publicvoidreadFile02()throwsIOException{
StringfilePath="D:\hacker.txt";
intreadData=0;
intreadLen=0;
//字节数组
byte[]bytes=newbyte[8];//一次最多读取8个字节FileInputStreamfileInputStream=null;
try{
fileInputStream=newFileInputStream(filePath);
//读文件,从字节数组中直接读取
//如果读取正常,返回实际读取的字节数
while((readLen=fileInputStream.read(bytes))!=-1){
System.out.print(newString(bytes,0,readLen));
}
}catch(IOExceptione){
thrownewRuntimeException(e);
}finally{
//关闭流资源
fileInputStream.close();
}
}
6.字节输出流FileOutputStream
使用示例:(向hacker.txt文件中加入数据)
/*
FileOutputStream数据写入文件
如果该文件不存在,则创建该文件
/
@Test
publicvoidwriteFile()throwsIOException{
StringfilePath="D:\hacker.txt";
FileOutputStreamfileOutputStream=null;
try{
fileOutputStream=newFileOutputStream(filePath);
//写入一个字节
//fileOutputStream.write('G');
//写入一个字符串
Strings="hellohacker!";
//fileOutputStream.write(s.getBytes());
//写入字符串指定范围的字符
fileOutputStream.write(s.getBytes(),0,3);
}catch(FileNotFoundExceptione){
thrownewRuntimeException(e);
}finally{
assertfileOutputStream!=null;
fileOutputStream.close();
}
}
我们发现,使用这样的FileOutputStream构造器无法实现向文件中追加内容,只能进行覆盖,我们可以在初始化对象的时候这样解决:
fileOutputStream=newFileOutputStream(filePath,true);
7.模拟文件拷贝
我们可以结合字节输入和输出流模拟一个文件拷贝的程序:
/*
文件拷贝
思路:
创建文件的输入流,将文件读取到程序
创建文件的输出流,将读取到的文件数据写入到指定的文件
/
@Test
publicvoidCopy()throwsIOException{
StringsrcFilePath="D:\hacker.txt";
StringdestFilePath="D:\hello.txt";
FileInputStreamfileInputStream=null;
FileOutputStreamfileOutputStream=null;
try{
fileInputStream=newFileInputStream(srcFilePath);
fileOutputStream=newFileOutputStream(destFilePath);
//定义字节数组,提高效率
byte[]buf=newbyte[1024];
intreadLen=0;
while((readLen=fileInputStream.read(buf))!=-1){
//边读边写
fileOutputStream.write(buf,0,readLen);
}
System.out.println("拷贝成功!");
}catch(IOExceptione){
thrownewRuntimeException(e);
}finally{
if(fileInputStream!=null){
fileInputStream.close();
}
if(fileOutputStream!=null){
fileOutputStream.close();
}
}
}
8.字符输入流FileReader
字符输入流FileReader用于从文件中读取数据
示例:
importjava.io.FileWriter;
importjava.io.IOException;/*
字符输出流
*/
publicclassFileWriteTest{
publicstaticvoidmain(String[]args)throwsIOException{
StringfilePath="D:\hacker.txt";
//创建对象
FileWriterfileWriter=null;
try{
char[]chars={'a','b','c'};
fileWriter=newFileWriter(filePath,true);
//写入单个字符
//fileWriter.write('H');
//写入字符数组
//fileWriter.write(chars);
//指定数组的范围写入
//fileWriter.write(chars,0,2);
//写入字符串
//fileWriter.write("解放军万岁!");
//指定字符串的写入范围
fileWriter.write("hackerclub",0,5);
}catch(IOExceptione){
thrownewRuntimeException(e);
}finally{
//FileWriter是需要强制关闭文件或刷新流的,否则数据会保存失败
fileWriter.close();
}
}
}
9.字符输出流FileWriter
字符输出流FileWrite用于写数据到文件中:
示例:
importjava.io.FileWriter;
importjava.io.IOException;/*
字符输出流
*/
publicclassFileWriteTest{
publicstaticvoidmain(String[]args)throwsIOException{
StringfilePath="D:\hacker.txt";
//创建对象
FileWriterfileWriter=null;
try{
char[]chars={'a','b','c'};
fileWriter=newFileWriter(filePath,true);
//写入单个字符
//fileWriter.write('H');
//写入字符数组
//fileWriter.write(chars);
//指定数组的范围写入
//fileWriter.write(chars,0,2);
//写入字符串
//fileWriter.write("解放军万岁!");
//指定字符串的写入范围
fileWriter.write("hackerclub",0,5);
}catch(IOExceptione){
thrownewRuntimeException(e);
}finally{
//FileWriter是需要强制关闭文件或刷新流的,否则数据会保存失败
fileWriter.close();
}
}
}
使用FileWriter,记得关闭文件或者刷新流!
读到这里,这篇“JavaI/O流如何使用”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
Java I/O流如何使用的详细内容,希望对您有所帮助,信息来源于网络。