JAVA IO流

来源:互联网 发布:xampp ubuntu安装 编辑:程序博客网 时间:2024/06/06 00:45

JAVAIO

一、文件操作

二、字节流

三、字符流

四、缓冲流

五、字节流与字符流的转换

 

一、文件操作

计算机中所有数据都是以文件的形式储存,如:图片文件,文本文件,音频文件等。不同的文件保存的数据不一样,字节组织形式不同,作用不同,但本质上都是以字节的形式保存。在JAVA中通过File类(java.io.file)来操作文件。

 

二、File类详解

 

AFile类构造方法

构造方法摘要

 

File(File parent,String child) 
根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。

 

 

File(String pathname) 
通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。

 

 

File(String parent,String child) 
根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。

 

 

File(URI uri) 
通过将给定的 file: URI 转换为一个抽象路径名来创建一个新的 File 实例。

 

 

 

BFile类变量

字段摘要

static String

pathSeparator 
与系统有关的路径分隔符,为了方便,它被表示为一个字符串。

static char

pathSeparatorChar 
与系统有关的路径分隔符。

static String

separator 
与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串。

static char

separatorChar 
与系统有关的默认名称分隔符。

 

CFile类方法

方法摘要

boolean

canExecute() 
测试应用程序是否可以执行此抽象路径名表示的文件。

boolean

canRead() 
测试应用程序是否可以读取此抽象路径名表示的文件。

boolean

canWrite() 
测试应用程序是否可以修改此抽象路径名表示的文件。

int

compareTo(File pathname) 
按字母顺序比较两个抽象路径名。

boolean

createNewFile() 
当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。

static File

createTempFile(String prefix,String suffix) 
在默认临时文件目录中创建一个空文件,使用给定前缀和后缀生成其名称。

static File

createTempFile(String prefix,String suffix,File directory) 
在指定目录中创建一个新的空文件,使用给定的前缀和后缀字符串生成其名称。

boolean

delete() 
删除此抽象路径名表示的文件或目录。

void

deleteOnExit() 
在虚拟机终止时,请求删除此抽象路径名表示的文件或目录。

boolean

equals(Object obj) 
测试此抽象路径名与给定对象是否相等。

boolean

exists() 
测试此抽象路径名表示的文件或目录是否存在。

File

getAbsoluteFile() 
返回此抽象路径名的绝对路径名形式。

String

getAbsolutePath() 
返回此抽象路径名的绝对路径名字符串。

File

getCanonicalFile() 
返回此抽象路径名的规范形式。

String

getCanonicalPath() 
返回此抽象路径名的规范路径名字符串。

long

getFreeSpace() 
返回此抽象路径名指定的分区中未分配的字节数。

String

getName() 
返回由此抽象路径名表示的文件或目录的名称。

String

getParent() 
返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null。

File

getParentFile() 
返回此抽象路径名父目录的抽象路径名;如果此路径名没有指定父目录,则返回 null。

String

getPath() 
将此抽象路径名转换为一个路径名字符串。

long

getTotalSpace() 
返回此抽象路径名指定的分区大小。

long

getUsableSpace() 
返回此抽象路径名指定的分区上可用于此虚拟机的字节数。

int

hashCode() 
计算此抽象路径名的哈希码。

boolean

isAbsolute() 
测试此抽象路径名是否为绝对路径名。

boolean

isDirectory() 
测试此抽象路径名表示的文件是否是一个目录。

boolean

isFile() 
测试此抽象路径名表示的文件是否是一个标准文件。

boolean

isHidden() 
测试此抽象路径名指定的文件是否是一个隐藏文件。

long

lastModified() 
返回此抽象路径名表示的文件最后一次被修改的时间。

long

length() 
返回由此抽象路径名表示的文件的长度。

String[]

list() 
返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。

String[]

list(FilenameFilter filter) 
返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中满足指定过滤器的文件和目录。

File[]

listFiles() 
返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。

File[]

listFiles(FileFilter filter) 
返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件和目录。

File[]

listFiles(FilenameFilter filter) 
返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件和目录。

static File[]

listRoots() 
列出可用的文件系统根。

boolean

mkdir() 
创建此抽象路径名指定的目录。

boolean

mkdirs() 
创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。

boolean

renameTo(File dest) 
重新命名此抽象路径名表示的文件。

boolean

setExecutable(boolean executable) 
设置此抽象路径名所有者执行权限的一个便捷方法。

boolean

setExecutable(boolean executable, boolean ownerOnly) 
设置此抽象路径名的所有者或所有用户的执行权限。

boolean

setLastModified(long time) 
设置此抽象路径名指定的文件或目录的最后一次修改时间。

boolean

setReadable(boolean readable) 
设置此抽象路径名所有者读权限的一个便捷方法。

boolean

setReadable(boolean readable, boolean ownerOnly) 
设置此抽象路径名的所有者或所有用户的读权限。

boolean

setReadOnly() 
标记此抽象路径名指定的文件或目录,从而只能对其进行读操作。

boolean

setWritable(boolean writable) 
设置此抽象路径名所有者写权限的一个便捷方法。

boolean

setWritable(boolean writable, boolean ownerOnly) 
设置此抽象路径名的所有者或所有用户的写权限。

String

toString() 
返回此抽象路径名的路径名字符串。

URI

toURI() 
构造一个表示此抽象路径名的 file: URI。

URL

toURL() 
已过时。 此方法不会自动转义 URL 中的非法字符。建议新的代码使用以下方式将抽象路径名转换为 URL:首先通过 toURI 方法将其转换为 URI,然后通过 URI.toURL 方法将 URI 装换为 URL。

 

三、文件常见操作举例

(1)创建文件/文件夹

(2)删除文件/文件夹

(3)查看文件/文件夹

 

A:文件常见操作示例

public class Demo1 {

public static void main(String[] args) throws IOException {

Demo1 d = new Demo1();

File file = new File("e://demo");

d.createFile(file);

File files = new File("e://demo//text.txt");

File files1 = new File("e://demo//text1.txt");

d.createFiles(files);

d.createFiles(files1);

d.selectFiles(file);

d.deleteFiles(files1);

}

/**

 * 创建文件夹

 * @throws IOException

 */

public void createFile(File file){

//判断文件夹是否存在

if(file.exists()){

System.out.println("文件夹已存在");

}else{

//创建文件

if(file.mkdirs()){

System.out.println("文件夹创建成功!");

}

}

}

/**

 * 创建文件

 * @throws IOException

 */

public void createFiles(File file) throws IOException{

if(file.exists()){

System.out.println("文件已存在!");

}else{

if(file.createNewFile()){

System.out.println("文件创建成功!");

}

}

}

/**

 * 遍历文件中所有文件/文件夹

 */

public void selectFiles(File file){

File[] files = file.listFiles();

for (File file2 : files) {

System.out.println(file2.getName());

System.out.println(file2.getPath());

}

}

/**

 * 删除文件/文件夹

 */

public void deleteFiles(File file){

if(file.exists()){

//删除文件/文件夹

if(file.delete()){

System.out.println("文件/文件夹删除成功!");

}

else{

System.out.println("文件/文件夹删除失败!");

}

}

else{

System.out.println("文件/文件夹不存在");

}

}

}

 

四、I/O流(input/output Stream

(1)i/o

Input(输入)表示将数据从硬盘读取到程序进行处理

        Output(输出)表示将数据从程序读取到硬盘进行处理

(2)stream:表示数据传输的媒介,类似在硬盘和程序之间的一个管道

(3)流的分类:

A、按数据流动的方向:输入流,输出流

B、按处理数据的大小:字节流,字符流

C、按功能不同:节点流,处理流

 

五、字节流

表示数据流动的最小单位为字节,通过两个超类来定义字节流:inputStream/outputStream

(1)inputStream类(抽象类,java.io.inputStream:该类不能直接使用,它有很多子类,常用FileInputStream子类来实现它。常用方法:colse()关闭当前流,read()读取数据的下一个字节,readbyte[])将字节读取到数组中。

 

(2)inputStream类示例

public class Demo2 {

public static void main(String[] args) throws IOException {

File file = new File("e://file//test.txt");

// Demo2.inputStreamFun(file);

// Demo2.inputStreamFun1(file);

Demo2.inputStreamFun2(file);

}

//输入流以一个字节为单位读取数据

public static void inputStreamFun(File file) throws IOException{

//FileInputSteam子类实现InputStream

InputStream input = new FileInputStream(file);

//read()方法读取数据

int count1 = input.read();

System.out.println((char)count1);

int count2 = input.read();

System.out.println((char)count2);

int count3 = input.read();

System.out.println((char)count3);

}

 

//输入流循环读取数据(效率低)

public static void inputStreamFun1(File file) throws IOException{

InputStream input = new FileInputStream(file);

int count = input.read();

//-1表示结尾

while(count != -1){

System.out.println((char)count);

count = input.read();

}

if(input != null){

//关闭连接,释放资源

input.close();

}

}

 

//输入流先将数据读取到byte[]数组

public static void inputStreamFun2(File file) throws IOException{

InputStream input = new FileInputStream(file);

//表示一次读取n个字节,这里是50,这个长度还是有将就的,你如果设置太小的话,可能又的汉字会出现乱码。

//即使你所读取文件的格式与编译器编码格式一致

byte[] bt = new byte[50];

//read(byte[])方法

int count = input.read(bt);

while(count != -1){

/*

byte[] bt = new byte[n];

String str = new String(byte[],offset,length);

字节数组装换成字符:offset起始下标,length字符数组长度

byte[] bt = str.getBytes()

字符串转换成字节数组

*/

String str = new String(bt,0,count);

System.out.println(str);

count = input.read(bt);

}

if(input != null){

//关闭连接,释放资源

input.close();

}

}

}

 

3outputStream类(抽象类,java.io.outputStream:该类不能直接使用,它有很多子类,常用FileOutputStream子类来实现它。常用方法:colse()关闭当前流,flush()刷新当前输出流,write()将指定字节写入当前输出流,writebyte[])将字节写入到数组中,然后加入到输出流,writebyte[],int,int)将指定字节数组从off开始的len个字节写入到当前输出流。

 

4outputStream类示例:

public class Demo2 {

public static void main(String[] args) throws IOException {

File file = new File("e://file//test.txt");

// Demo2.outputStreamFun(file);

// Demo2.outputStreamFun1(file);

File file1 = new File("e://file//test1.txt");

fileCopy(file,file1);

}

//给指定文件写入内容(效率低)

public static void outputStreamFun(File file) throws IOException{

//true表示是否可以向指定文件追加内容

OutputStream output = new FileOutputStream(file,true);

char count ='1';

//写入内容

output.write(count);

//刷新输出流

output.flush();

//关闭连接,释放资源

output.close();

}

//将字符串转换为字节写入文件

public static void outputStreamFun1(File file) throws IOException{

//true表示是否可以向指定文件追加内容

OutputStream output = new FileOutputStream(file,true);

String str = "今天,天气真好";

//写入内容

output.write(str.getBytes());

//刷新输出流

output.flush();

//关闭连接,释放资源

output.close();

}

//文件复制

public static void fileCopy(File file,File file1) throws IOException{

InputStream input = new FileInputStream(file);

OutputStream output = new FileOutputStream(file1,true);

//一个字节数组

byte[] bt = new byte[1024];

int count = input.read(bt);

while(count != -1){

String str = new String(bt, 0, count);

System.out.println(str);

output.write(bt);

count = input.read(bt);

}

//先关闭输出流,再关闭输入流

output.flush();

output.close();

input.close();

}

}

 

六、字符流

字符流是一种经过编码、加工后的字节流,字符流的本质还是字节流,只是更方便我们对字符进行操作处理。字符流是由字符组成的,包括字符、字符串和字符数组等。字符流处理由两个字节构成Unicode码。一般的文本文件(.txt)就使用使用字符流来处理。通过两个超类来定义字符流:ReaderWriter

 

1Reader类(抽象类,java.io.Reader):该类不能直接使用,它有很多子类,常用FileReader子类来实现它。

 

2Reader类示例:

public class Demo3 {

public static void main(String[] args) throws IOException {

File file = new File("e://file//test.txt");

//Demo3.fileReaderFun(file);

//Demo3.fileReaderFun1(file);

}

 

//输入字符流read(),单个字符读入效率低

public static void fileReaderFun(File file) throws IOException{

Reader reader = new FileReader(file);

int count = reader.read();

while(count != -1){

System.out.println((char)count);

count = reader.read();

}

if(reader != null){

reader.close();

}

}

//输入字符流read(char[]),先读入到字符数组中,效率高些

public static void fileReaderFun1(File file) throws IOException{

Reader reader = new FileReader(file);

char ch[] = new char[1024];

int count = reader.read(ch);

while(count != -1){

String str = new String(ch, 0, count);

System.out.println(str);

count = reader.read();

}

if(reader != null){

reader.close();

}

}

}

 

3Writer类(抽象类,java.io.Writer:该类不能直接使用,它有很多子类,常用FileWriter子类来实现它。

 

(4)Writer类示例

public class Demo3 {

public static void main(String[] args) throws IOException {

File file = new File("e://file//test.txt");

File file1 = new File("e://file//test1.txt");

//Demo3.fileReaderFun(file);

//Demo3.fileReaderFun1(file);

//filewriterFun(file);

//test.txt文件里的内容复制到test1.txt

fileCopy(file,file1);

}

//输出字符流writer(),单个字符写出

public static void filewriterFun(File file) throws IOException{

//写入数据需要设置参数为true,不然每次写入的内容将会覆盖原有的内容

Writer writer = new FileWriter(file,true);

String str ="abcd";

writer.write(str);

writer.flush();

writer.close();

}

//文件复制

public static void fileCopy(File file,File file1) throws IOException{

Reader reader = new FileReader(file);

Writer writer = new FileWriter(file1);

char ch[] = new char[1024];

int count = reader.read(ch);

while(count != -1){

String str = new String(ch, 0, count);

System.out.println(str);

writer.write(ch);

count = reader.read(ch);

}

writer.flush();

writer.close();

reader.close();

}

}

 

 

 

七、缓冲流

缓冲流就是一块内存区域,是更高级别的字符流。目的是为了加快读写的速度,缓冲流是先将内容保存到缓冲区中,缓冲区满了后一次性完成读写操作,这样能大大加快数据的读写速度。通过输入/输出字符流的两个子类来实现:BufferedReader(输入缓冲流),PrinterWriter(输出缓冲流)。

 

(1)示例

public class Demo4 {

public static void main(String[] args) throws IOException {

File file = new File("e://file//test.txt");

File file1 = new File("e://file//test1.txt");

Demo4.fileCopy(file, file1);

}

//使用缓冲流复制文件

public static void fileCopy(File file,File file1) throws IOException{

//使用字符流

Reader reader = new FileReader(file);

Writer writer = new FileWriter(file1,true);

//字符流包装成缓冲流

BufferedReader br = new BufferedReader(reader);

PrintWriter pw = new PrintWriter(writer,true);

//读取一行

String str = br.readLine();

while(str != null){

System.out.println(str+"\r\n");

pw.write(str);

str = br.readLine();

}

pw.flush();

pw.close();

br.close();

}

}

 

八、字节流与字符流的转换

1)输入字节流转换为输入字符流

//字节输入流

FileInputStream input = new FileInputStream(“file”);

//中间输入流(InputStreamReaderReader类的子类,FileReader类的父类)

InputStreamReader isr = new InputStreamReader(input);

//字符输入流

BufferedReader br = new BufferedReader(isr);

 

2)输出字节流转换为输出字符流

//字节输出流

FileOutputStream input = new FileOutputStream(“file”);

//中间输出流(OutputStreamWriterWriter类的子类,FileWriter类的父类)

OutputStreamWriter isr = new OutputStreamWriter (input);

//字符输出流

PrintWriter br = new PrintWriter(isr);

 

 

 

0 0
原创粉丝点击