java基础学习之IO

时间:2019-03-18
本文章向大家介绍java基础学习之IO,主要包括java基础学习之IO使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

java的IO是实现输入和输出的基础,IO可以方便的实现数据的输入和输出操作。通过流的形式允许java程序使用相同的方式来访问不同的输入/输出源。
常用的类以及方法如下:

  1. File(String pathname)
    通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例
    例如:
String path="F:\\2019\\课程\\2019内民族\\3-18\\3-18.txt";
File file=new File(path);
  1. boolean exists()
    测试此抽象路径名表示的文件或目录是否存在
    例如;
 if(file.exists()) {
		  System.out.println("文件存在");
	  }
  1. boolean createNewFile()
    当且仅当具有该名称的文件尚不存在时,原子地创建一个由该抽象路径名命名的新的空文件
    例如:
String path="C:\\Users\\张宏庆\\Desktop\\cc.txt";
File  file=new File(path);
try {
		file.createNewFile();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

4 boolean mkdir()
创建由此抽象路径名命名的目录
例如:

String path="C:\\Users\\张宏庆\\Desktop\\dd";
File  file=new File(path);
	try {
		file.mkdir();
		System.out.println("创建成功");
	} catch (Exception e) {
		// TODO: handle exception
	}
  1. boolean isDirectory()
    测试此抽象路径名表示的文件是否为目录
    例如:
File[] files=file.listFiles();
for(File f:files) {//循环遍历files中的元素
			  if(f.isDirectory()) {
				 System.out.println("目录");
			  }
  1. boolean isFile()
    测试此抽象路径名表示的文件是否为普通文件
File[] files=file.listFiles();
for(File f:files) {//循环遍历files中的元素
			  if(f.isFile()) {
				 System.out.println("文件");
			  }

7、File[] listFiles()
返回一个抽象路径名数组,表示由该抽象路径名表示的目录中的文件

    File[] files=file.listFiles();

8、String getPath()
将此抽象路径名转换为路径名字符串

f.getPath();

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

f.getAbsolutePath() ;

10、boolean delete()
删除由此抽象路径名表示的文件或目录

String path="C:\\Users\\张宏庆\\Desktop\\cc.txt";
File  file=new File(path);
	try {
		file.delete();
		System.out.println("删除成功");
	} catch (Exception e) {
		// TODO: handle exception
	}

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

f.getName();

关于字节流和字符流

字节流
public abstract class InputStream extends Object implements Closeable
方法:
int read(byte[] b)
从输入流读取一些字节数,并将它们存储到缓冲区 b
字节数组返回值,一定会出现半桶水和空桶的现象。int表示水桶返回的字节数量,空桶表示-1
例如:

String path="F:\\2019\\课程\\2019内民族\\3-18\\3-18.txt";
File file=new File(pathName);
	try {
		InputStream input=new FileInputStream(file);
		byte[] buffer=new byte[1024];
		int len;
		while((len=input.read(buffer))!=-1) {
			System.out.println("读取字节数为:"+len);
		}
	} catch (Exception e) {
		// TODO: handle exception
	}

void write(byte[] b, int off, int len)
从指定的字节数组写入 len个字节,从偏移 off开始输出到此输出流。
例如:

String fileSrc="C:\\Users\\张宏庆\\Desktop\\aa.txt";
String fileDest="C:\\Users\\张宏庆\\Desktop\\bb.txt";
File src=new File(fileSrc);
	File dest=new File(fileDest);
	try {
		InputStream input=new FileInputStream(src);
		OutputStream output=new FileOutputStream(dest);
		byte[] buffer=new byte[1024];
		int len;
		while((len=input.read(buffer))!=-1) {
			System.out.println("读取字节数:" + len);
			output.write(buffer, 0, len);		
		}
	} catch (Exception e) {
		// TODO: handle exception
	}

字符流:
public abstract class Reade extends Object implements Readable, Closeable
FileReader(File file)
创建一个新的 FileReader ,给出 File读取
public class BufferedReader extends Reader 通过缓冲的方式,优化文本信息的读取
String readLine() 读一行文字。
void println(String x)
打印一个字符串,然后终止行。
例如:

String fileSrc="C:\\Users\\张宏庆\\Desktop\\aa.txt";
String fileDest="C:\\Users\\张宏庆\\Desktop\\bb.txt";
File src=new File(fileSrc);
	File dest=new File(fileDest);
	try {
		BufferedReader in=new BufferedReader(new FileReader(src));
		PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(dest)));
		String line;
		while((line=in.readLine())!=null) {
			out.println(line);
		}
		in.close();
		out.close();
		System.out.println("拷贝完成");
	} catch (Exception e) {
		// TODO: handle exception
	}