java字符文件流有哪些_java系列17:File文件类、字节流、字符流

一、File文件类

Java要操作文件,先要把文件看作对象。

例1:创建目录和文件

package test;

import java.io.File;

import java.io.IOException;

public class TestFile {

public static void main(String[] args) {

File file = new File("aaa");

if(!file.exists()) {

file.mkdir(); // 创建目录

}

File a = new File(file, "a.txt");

if(!a.exists()) {

try {

a.createNewFile(); // 创建文件

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

43e25e487216

运行结果:创建了一个目录和文件

例2:获取文件的属性

package test;

import java.io.File;

public class TestFile2 {

public static void main(String[] args) {

File file = new File("aaa/a.txt");

System.out.println(file.getName()); // 文件的名称

System.out.println(file.getAbsolutePath()); // 文件的绝对路径

System.out.println(file.length()); // 文件的大小

System.out.println(file.exists()); // 文件是否存在

System.out.println(file.canRead()); // 是否可读

System.out.println(file.canWrite()); // 是否可写

// System.out.println(file.delete()); // 删除文件

File[] files = file.listFiles(); // 获得目录下所有的子目录和文件

}

}

43e25e487216

运行结果

例3:listFiles()方法,获得目录下所有的子目录和文件

package test;

import java.io.File;

/**

* 打印出G盘的所有文件

* @author xiangdonglee

*

*/

public class TestFile3 {

public static void main(String[] args) {

File file = new File("G:\\Downloads");

File[] files = file.listFiles();

printAllFile(files);

}

public static void printAllFile(File[] files) {

for (int i = 0; i < files.length; i++) {

if(files[i].isFile()) {

System.out.println(files[i].getAbsolutePath());

} else {

File[] files1 = files[i].listFiles();

printAllFile(files1); // 递归遍历目录

}

}

}

}

43e25e487216

运行结果

二、IO

作用:

操作系统文件(读、写)

用于网络数据的传输

流:用来操作系统文件的工具。

分为:输入流、输出流

从内容上分:字节流、字符流

在使用流的时候,必须有开有关。

三、字节流

1、FileInputStream

例1:

package test;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

public class TestIO {

public static void main(String[] args) {

File file = new File("aaa/a.txt");

FileInputStream fis = null;

if(file.exists()) {

try {

fis = new FileInputStream(file);

// 使用文件输入流,一个字符一个字符地读取数据

// 效率很低,主要学习如何查看帮助文档,自己解决问题

for (int i = 0; i < file.length(); i++) {

System.out.print((char)fis.read());

}

fis.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

43e25e487216

先在aaa.txt中写入内容

43e25e487216

运行结果

例2:

package test;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

public class TestIO2 {

public static void main(String[] args) {

File file = new File("aaa/a.txt");

FileInputStream fis = null;

if(file.exists()) {

try {

fis = new FileInputStream(file);

byte[] buf = new byte[6];

while(fis.read(buf) != -1) {

// System.out.println(buf); // 数组打印出来是地址

System.out.println(new String(buf));

}

fis.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

43e25e487216

为什么是这样的结果?

例3:

package test;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

/**

* 字符流最高效的读法

* @author xiangdonglee

*

*/

public class TestIO3 {

public static void main(String[] args) {

File file = new File("aaa/a.txt");

FileInputStream fis = null;

if(file.exists()) {

try {

fis = new FileInputStream(file);

byte[] buf = new byte[6];

int len = 0;

while((len = fis.read(buf)) != -1) {

// System.out.println(buf); // 数组打印出来是地址

System.out.println(new String(buf, 0, len));

}

fis.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

43e25e487216

运行结果

2、FileOutputStream

例1:

package test;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.util.Scanner;

/**

* 输出流往文件中写

* @author xiangdonglee

*

*/

public class TestIO4 {

public static void main(String[] args) {

File file = new File("aaa/a.txt");

FileOutputStream fos = null;

Scanner scan = new Scanner(System.in);

if(file.exists()) {

try {

fos = new FileOutputStream(file);

System.out.println("请输入您想说的话:");

String say = scan.nextLine();

fos.write(say.getBytes());

fos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

43e25e487216

输入hello java

43e25e487216

在a.txt中写入了hello java

例2:请用户输入任何内容到a.txt中,直到输入88结束

package test;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.util.Scanner;

/**

* 请用户输入任何内容到a.txt中,直到输入88结束

* @author xiangdonglee

*

*/

public class TestIO5 {

public static void main(String[] args) {

File file = new File("aaa/a.txt");

FileOutputStream fos = null;

Scanner scan = new Scanner(System.in);

if(file.exists()) {

try {

fos = new FileOutputStream(file);

System.out.println("请输入您想说的内容:");

while(true) {

String say = scan.nextLine();

if(say.equals("88")) {

break;

}

fos.write((say + "\r\n").getBytes());

}

fos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

43e25e487216

运行结果

四、字符流

1、FileReader

例:

43e25e487216

读取D盘下hello_java.txt中的内容

package test;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

/**

* 读取D盘下hello_java.txt中的内容

* @author xiangdonglee

*

*/

public class TestFileReader {

public static void main(String[] args) {

File file = new File("D:/hello_java.txt");

FileReader fr = null;

char[] chary = new char[10];

int len = 0;

if(file.exists()) {

try {

fr = new FileReader(file);

while((len = fr.read(chary)) != -1) {

System.out.println(new String(chary, 0, len));

}

fr.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

43e25e487216

运行结果

2、FileWriter

例:

package test;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

public class TestFileWriter {

public static void main(String[] args) {

File file = new File("b.txt");

if(!file.exists()) {

try {

file.createNewFile();

} catch (Exception e) {

e.printStackTrace();

}

}

try {

FileWriter fw = new FileWriter(file);

System.out.println("请输入您想说的内容:");

Scanner scan = new Scanner(System.in);

String say = scan.nextLine();

fw.write(say);

fw.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

43e25e487216

运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值