您的位置:首页 > 编程语言 > Java开发

---java IO总结

2016-07-15 16:17 537 查看

File类

File类用来表示一个文件或者目录

public class TestFile {

/*
* java.io.File
* 1.凡是输入,输出等IO操作相关的 类,接口,都定义在java.io包下
* 2.File是一个类。可以用构造函数创建一个File类来表示一个文件或者文件目录
* 3.File是平台无关的。
* 4.File只能用来表示文件或者文件目录,只涉及到文件或者文件目录的创建,删除,重命名等,不涉及到文件内容的操作(需要由IO流来完成)
* 5.File通常用来作为Io流具体类的形参
*/
@Test
public void test3() throws Exception {
/*
* createNewFile() //创建一个新的
* delete() //删除File
* mkDir():创建一个文件目录。只有在上层文件目录存在的情况下,才能返回true
* mkDirs():创建一个文件目录。若上层文件目录不存在,一并创建 list() listFiles()
*/
File file = new File("test3.txt");
System.out.println(file.delete());

if (!file.exists()) {
boolean b = file.createNewFile();
System.out.println(b);

}

File file2 = new File("file2");
if (!file2.exists()) {
boolean b = file2.mkdirs();
System.out.println(b);
}
// 列出文件目录下的文件
File file3 = new File("file2");
String[] str = file3.list();
for (String string : str) {
System.out.println(string);
}

File[] f = file3.listFiles();
for (File file4 : f) {
System.out.println(file4.getName());
}

}

@Test
public void test2() {
/*
* exists() //文件或者目录是否存在
* canWrite() //是否可以写
* canRead() //是否可以读
* isFile()//是否是一个文件
*  isDirectory()//是否是一个目录
*  lastModified()//最后修改的时间
* length()//文件大小
*
*/
File file1 = new File("file.txt");
File file2 = new File("hello.txt");

System.out.println(file1.exists());
System.out.println(file1.canWrite());
System.out.println(file1.canRead());
System.out.println(file1.isFile());
System.out.println(file1.isDirectory());
System.out.println(new Date(file1.lastModified()));
System.out.println(file1.length());

System.out.println();

System.out.println(file2.exists());
System.out.println(file2.canWrite());
System.out.println(file2.canRead());
System.out.println(file2.isFile());
System.out.println(file2.isDirectory());
System.out.println(new Date(file2.lastModified()));
System.out.println(file2.length());

}

@SuppressWarnings("unused")
@Test
public void test1() {
File file1 = new File("D:\\hello.txt");// 或者windows下可以直接写D:/hello.txt
File file2 = new File("hello1.txt");

File file3 = new File("D:\\IO\\IO1");
File file4 = new File("D:\\IO2");

System.out.println(file1.getName());// 得到文件名称
System.out.println(file1.getPath());// 得到文件路径
System.out.println(file1.getAbsolutePath());// 得到文件的绝对路径
System.out.println(file1.getParent());// 得到文件父目录
System.out.println(file1.getAbsoluteFile());// 得到绝对文件名称

System.out.println();

System.out.println(file3.getName());
System.out.println(file3.getPath());
System.out.println(file3.getAbsoluteFile());
System.out.println(file3.getParent());
System.out.println(file3.getAbsolutePath());

// renameTo
// renameTo(File newName):重命名
// file1.renameTo(file2):file1重命名为file2.要求:file1文件一定存在,file2一定不存在
boolean b = file1.renameTo(file2);// file1存在于磁盘D:\\hello.txt,重命名目标为file2,file2位于相对本目录下执行后file1会renameTo到file2
System.out.println(b);
}
}


java IO体系

/*
* 1.流的分类:
* 按照数据流向的不同:输入流  输出流
* 按照处理数据的单位的不同:字节流  字符流(处理的文本文件)
* 按照角色的不同:节点流(直接作用于文件的)  处理流
*
* 2.IO的体系
* 抽象基类         节点流(文件流)                                缓冲流(处理流的一种)
* InputStream      FileInputStream         BufferedInputStream
* OutputStream     FileOutputStream        BufferedOutputStream
* Reader           FileReader              BufferedReader
* Writer           FileWriter              BufferedWriter
*
* IO体系:
*      ||--字符流
*          ||--Reader
*              ||--BufferedReader
*              ||--InputStreamReader=====FileReader
*
*          ||--Writer
*              ||--BufferedWriter
*              ||--OutputStreamWriter=====FileWriter
*      ||--字节流
*          ||--InputStream
*              ||--FileInputStream
*              ||--filterInputStream=====BufferedInputStream
*
*          ||--OutputStream
*              ||--FileOutputStream
*              ||--FilterOutputStream=====BufferedOutputStream
*/


public class TestFileInputOutputStream {

@Test
public void testCopyfile() {
long start = System.currentTimeMillis();
String src = "C:\\Users\\tuxianchao\\Desktop\\1.wmv";
String dest = "C:\\Users\\tuxianchao\\Desktop\\2.wmv";
CopyFile(src, dest);
long end = System.currentTimeMillis();
System.out.println("花费的时间:" + (end - start));
}

// 实现文件复制的方法
public void CopyFile(String src, String dest) {
// 2.提供相应的流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// 1.提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
// 3.实现文件的复制,读取文件并切入到目标文件
byte[] b = new byte[10];
int len;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

}

@Test
// 从硬盘读取一个文件,并写入到另一个位置。(相当于文件的复制)
public void testFileInputOutputStream() {
// 2.提供相应的流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// 1.提供读入、写出的文件
File file1 = new File("C:\\Users\\tuxianchao\\Desktop\\1.png");
File file2 = new File("C:\\Users\\tuxianchao\\Desktop\\2.png");
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
// 3.实现文件的复制,读取文件并切入到目标文件
byte[] b = new byte[20];
int len;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

}

@Test
// 写入内容到文件
public void testFileOutStream() {

// 2.创建一个FileOutputStream的对象,将file的对象作为形参传递给FileOutputStream的构造器中
FileOutputStream fos = null;
try {
// 1.创建一个File对象,表明要写入的文件位置。
// 输出的物理文件可以不存在,当执行过程中,若不存在,会自动的创建。若存在,会将原有的文件覆盖
File file = new File("file4.txt");

fos = new FileOutputStream(file);
// 3.写入操作
// 将字符串转化为字符数组
byte[] b = new String("I Love China!!!").getBytes();
fos.write(b);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 关闭写入流
if (fos != null) {

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

@Test
public void testFileInputStream3() {
FileInputStream fis = null;
try {
File file = new File("file.txt");
fis = new FileInputStream(file);
byte[] b = new byte[5];// 读取到的数据要写入的数组。
int len;// 每次读入到byte中的字节的长度
while ((len = fis.read(b)) != -1) {
// for (int i = 0; i < len; i++) {
// System.out.print((char) b[i]);
// }
String str = new String(b, 0, len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

@Test
// 对方式一的改进,使用try catch语句更为合理
public void testFileInputStream2() {
// 2.创建一个FileInputStream对象
FileInputStream fis = null;
try {
// 1.创建一个文件对象
File file = new File("file.txt");
fis = new FileInputStream(file);
// 3.调用FileInputStream的方法,实现file文件的读取。
int b;
while ((b = fis.read()) != -1) {
System.out.print((char) b);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// fially语句中无论如何保证流的关闭
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

// 从硬盘文件读取数据到程序中,读取数据的文件一定要存在,
// 否则会报异常FileNotFoundException
@Test
public void testFileInputStream1() throws Exception {

// 1.创建一个文件对象
File file = new File("file.txt");
// 2.创建一个FileInputStream对象
FileInputStream fis = new FileInputStream(file);
/*
* read()读取到文件末尾时,返回-1
*/
int b;
while ((b = fis.read()) != -1) {
System.out.print((char) b);
}
// 3.关闭对应的流
fis.close();
}

}


public class TestFileReaderWriter {
/*
* 使用FileReader,FileWriter(字符流)来处理文本文件
*
* 对于非文本文件(视频,图片等),只能使用字节流
*/
@Test
// 实现一个文本文件的复制,不能用作非文本文件的复制
public void testFileReaderWriter() {

FileReader fr = null;
FileWriter fw = null;
try {
File file1 = new File("dbcp.txt");
File file2 = new File("dbcp2.txt");

fr = new FileReader(file1);
fw = new FileWriter(file2);

char[] c = new char[20];
int len;
while ((len = fr.read(c)) != -1) {
fw.write(c, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

@Test
public void testFileWriter() {
FileWriter fw = null;
try {
File file = new File("dbcd1.txt");
fw = new FileWriter(file);
String str = new String("I Love china!!!!!");
fw.write(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fw != null) {

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

}

@Test
public void testFileReader() {
// TODO Auto-generated method stub
FileReader fr = null;
try {
File file = new File("dbcp.txt");
fr = new FileReader(file);
char[] c = new char[10];
int len;
while ((len = fr.read(c)) != -1) {
String str = new String(c, 0, len);
System.out.print(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}


public class Testbuffered {

@Test
//使用BufferedReaderWriter来实现文本文件的复制
public void testBuferedReaderWriter() {
FileReader fr = null;
FileWriter fw = null;
BufferedWriter bw = null;
BufferedReader br = null;
try {
File file1 = new File("dbcp.txt");
File file2 = new File("dbcp4.txt");

fr = new FileReader(file1);
fw = new FileWriter(file2);

br = new BufferedReader(fr);
bw = new BufferedWriter(fw);

// char[] c = new char[1024];
// int len;
// while ((len = br.read(c)) != -1) {
// bw.write(c, 0, len);
// bw.flush();
// }
String str;
while ((str = br.readLine()) != null) {
// bw.write(str+"\n");
bw.write(str);
bw.newLine();
bw.flush();

}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bw != null) {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (br != null) {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

@Test
public void testCopyFile() {
long start = System.currentTimeMillis();
// String src = "C:\\Users\\tuxianchao\\Desktop\\image.jpg";
// String dest = "C:\\Users\\tuxianchao\\Desktop\\image3.jpg";
String src = "C:\\Users\\tuxianchao\\Desktop\\1.wmv";
String dest = "C:\\Users\\tuxianchao\\Desktop\\2.wmv";

CopyFile(src, dest);
long end = System.currentTimeMillis();
System.out.println("花费的时间:" + (end - start));
}

// 使用缓冲流实现文件的复制的方法
public void CopyFile(String src, String dest) {
FileOutputStream fos = null;
FileInputStream fis = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File file1 = new File(src);
File file2 = new File(dest);

fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);

bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] b = new byte[4096];
int len;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
bos.flush();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

@Test
// 使用BufferedInputStream和BufferedOutputStream实现非文本文件的复制
public void testBufferedInputOutputStream() {
FileOutputStream fos = null;
FileInputStream fis = null;
// 3.将创建节点流的对象作为形参传递给缓冲流的构造器中
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 1.提供读入、写出的文件
// File file1 = new File("dbcp.txt");
// File file2 = new File("dbcp3.txt");
File file1 = new File("C:\\Users\\tuxianchao\\Desktop\\image.jpg");
File file2 = new File("C:\\Users\\tuxianchao\\Desktop\\image1.jpg");

// 2.先创建相应的节点流:FileInputStream、FileOutputStream
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);

bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);

// 4.具体的实现文件复制的操作
byte[] b = new byte[10];
int len;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
bos.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
}


如何实现字节流与字符流之间的转换:

转换流:InputStreamReader OutputStreamWriter

编码:字符串 --->字节数组

解码:字节数组--->字符串


@Test
public void test1() {

BufferedReader br = null;
BufferedWriter bw = null;
try {
// 输入到程序的时候将字节数组转换为字符串(字节流--->字符流)
File file1 = new File("dbcp.txt");
FileInputStream fis = new FileInputStream(file1);
InputStreamReader isr = new InputStreamReader(fis, "GBK");
br = new BufferedReader(isr);

// 输出到文件的时候将字符串转换为字节数组(字符流--->字节流)
File file2 = new File("dbcp5.txt");
FileOutputStream fos = new FileOutputStream(file2);
OutputStreamWriter osr = new OutputStreamWriter(fos, "GBK");
bw = new BufferedWriter(osr);

String str;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
bw.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java-io