0
点赞
收藏
分享

微信扫一扫

File和File的IO流

念川LNSC 2022-08-19 阅读 76


目录

File的使用

File的构造方法:

File和File的IO流_数组


File的使用格式:

File file=new File("d:\\12.txt");

File的使用举例:

//创建一个File类型的对象
File file=new File("d:\\12.txt");
//判断是否存在12.txt这个文件,若不存在则进行新建
if(!file.exists()){
try {
file.createNewFile();
} catch

File的IO流

不带缓冲的FileIO流

File的IO流包括:FileInputStream和FileOutputStream。这个类都是以字节byte为单位进行读写的,因此只能使用byte[] array2=new byte[2^n];来读入或者写出。

FileInputStream的构造方法:

File和File的IO流_io流_02


FileInputStream的使用:

new File("d:\\12.txt");//创建文件
byte[] array1=new byte[1024];//2的n次方个字节
try {
//创建文件读入流
FileInputStream ifile=new FileInputStream(file);
/*读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。 */
int num=ifile.read(array1);
if(num!=-1)
{
System.out.println(new String (array1,0,num));
num=ifile.read(array1);
}
ifile.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block

File和File的IO流_io流_03


以上程序存在弊端:受array数组的影响,只能读1024个字节,当12.txt中存储文章更多时,不能输出全部。如何输出文件中的全部内容将在本文后面列出。

FileOutputStream的构造方法:

File和File的IO流_io流_04


FileInputStream的使用:

new File("d:\\12.txt");
String s="正在写入中";
try {
byte[] array=s.getBytes();//byte封装类的使用
FileOutputStream ofile=new
FileOutputStream(file);
//写出
ofile.write(array);
ofile.flush();//注意所有的写出操作最后都要写出缓存
ofile.close();//关闭文件
System.out.println("正常");


} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block

注意:
1、 inputstream//读入
   outputstream//写出
2、区别InputStream 与InputStreamReader
InputStream 此抽象类是表示字节输入流的所有类的超类
InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,否则可能接受平台默认的字符集。
每次调用 InputStreamReader 中的一个 read() 方法都会导致从基础输入流读取一个或多个字节。

带缓冲的FileIO流

BufferedReader

BufferedReader:从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
步骤:
新建File类型的文件:File(String pathname)
创建文件输入流FileInputStream:FileInputStream(File file)
创建读入流InputStreamReader:InputStream()
创建BufferedReader

带缓冲的读入流

package readers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class Readersliu

public static void main(String[] args) {
File file=new File("d:\\111.txt");
try {
FileInputStream fis=new FileInputStream(file);
InputStreamReader reader=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(reader);
String line=br.readLine();
//带有buffer的io流是以字符为单位进行读写操作的,所以是readline()
while(line!=null){
System.out.println(line);
line=br.readLine();
}
br.close();
reader.close();
fis.close();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block

出现问题:InputStreamreader不是InputStream

BufferedWriter

BufferedWriter:将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
步骤与读入类似。

File file = new File("d:12.txt");
try {
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
bw.write("啊哈哈哈哈哈哈哈哈哈哈哈");//写出内容
bw.flush();//输出缓冲
bw.close();//关闭缓冲
osw.close();
fos.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

注意问题:
1、在关闭之前需要有文件名.flush()
2、加IOexception时选择第二个Add catch clause surroundering try
3、有时会有乱码,乱码原因是乱码原因:Eclipce使用 UTF-8(国际标准)或GBK/GBK2312标准,中文的显示采用ASCII码,解决方法:可以采用UTF-8来显示

附加例子

字节方式:

从文件12.txt写出到文件22.txt    
File file=new File("d:\\12.txt");//读入文件
File filecopy=new File("d:\\22.txt");//写出文件
try {
FileInputStream fis=new FileInputStream(file);
//先读入我脑中
FileOutputStream fos=new FileOutputStream(filecopy);
//再写出到文件中
byte[] array=new byte[1024];
int num=fis.read(array);//读入array字节数组
while(num!=-1){
fos.write(array,0,num);//写出array字节数组
num=fis.read();//再次读
}
fos.flush();
fis.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch

字符方式

=new File("d:\\12.txt");//读入文件
File filecopy=new File("d:\\22.txt");//写出文件
try {
FileInputStream fis=new FileInputStream(file);
FileOutputStream fos=new FileOutputStream(filecopy);
InputStreamReader ir=new InputStreamReader(fis);
OutputStreamWriter ow=new OutputStreamWriter(fos);
BufferedReader br=new BufferedReader(ir);
BufferedWriter bw=new BufferedWriter(ow);
String line=br.readLine();
while(line!=null)
{
bw.write(line);
line=br.readLine();
}
bw.flush();
bw.close();
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


举报

相关推荐

File类和IO流

IO流:File类

FILE类与IO流

JAVA-File、iO流

File类、IO流(部分)

0 条评论