package com.ydlclass.feature;
import org.junit.Test;
import java.io.*;
public class IOTest {
String tarPath;
public String getTarPath() {
return tarPath;
}
public void setTarPath(String tarPath) {
this.tarPath = tarPath;
}
//文件流的使用步骤,例如读取a.t
@Test
public void testRead1() throws FileNotFoundException {
String path = "E:\\元气壁纸缓存\\img_cache\\a.txt";
File file = new File(path);
FileInputStream fileInputStream = new FileInputStream(file);//使用一个文件输入流,对接到文件上面去
int read;
int i = 0;
byte[] bytes = new byte[10];
try {
while ((read = fileInputStream.read()) != -1){
byte b = (byte)read;
bytes[i] = b;
i++;
}
System.out.println(new String(bytes));
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
//第二个read方法的使用以及返回值的含义,参数中需要一个byte数组保存起来;返回值是读取的字节个数
public void testRead2() throws IOException {
String path = "E:\\元气壁纸缓存\\img_cache\\a.txt";
File file = new File(path);
InputStream inputStream = new FileInputStream(file);
byte[] bytes = new byte[10];
int read;
while ((read = inputStream.read(bytes)) != -1){
System.out.println(new String(bytes,0,bytes.length - 1));//使用这种方式避免字符串之后的为乱码
}
//替换掉上面的内容
//String path1 = "E:\\元气壁纸缓存\\img_cache\\a.txt";
//File file1 = new File(path);
//InputStream inputStream1 = new FileInputStream(file);
//byte[] bytes1 = new byte[10];
//int read1;
//StringBuilder sb = new StringBuilder();
//while ((read1 = inputStream.read(bytes)) != -1){
// //每次读出多少个字节,就将多少个字节变为字符串
// String s = new String(bytes,0,read1);
// sb.append(s);
//}
//System.out.println(sb);
}
@Test
public void testOutput() throws IOException {
byte[] bytes = {65,66,67};
//FileOutputStream这个构造器有重载方法,这个重载方***填入一个布尔值,
// 不加true,则默认覆盖追加至文件的,false则会覆盖源文件内容
OutputStream outputStream = new FileOutputStream("E:\\元气壁纸缓存" +
"\\img_cache\\a.txt",false);
//制定向哪个文件输出
//outputStream.write(65);//输出的内容为byte类型的值A
//outputStream.write("hello world!".getBytes());
//
outputStream.write(bytes);
}
@Test
//尝试将一个mv视频文件使用io流的方式从磁盘复制到指定的磁盘目录下
public void testCopyFile() throws IOException {
//首先使用输入流将文件读入到内存中
//使用此函数找出哪些是mv文件
//String PathOfFiles = findMvFiles("E:\\CloudMusic");
//System.out.println(PathOfFiles);
//InputStream inputStream = new FileInputStream(new File("E:\\CloudMusic\\MV\\焦迈奇 - 我的名字.mp4"));
//byte[] bytes = new byte[9000000];
//inputStream.read(bytes);
//
////将mv文件输出至制定的目录中
//File tempfile = new File("E:\\CloudMusic\\skin\\temp.mp4");
//boolean exists = tempfile.exists();
//if(!exists){//创建一个临时mp4文件
// tempfile.createNewFile();
//}
//OutputStream outputStream = new FileOutputStream(tempfile.getAbsolutePath());
//outputStream.write(bytes);
//InputStream inputStream = new FileInputStream("E:\\CloudMusic\\MV\\焦迈奇 - 我的名字.mp4");
//File file = new File("E:\\CloudMusic\\MV\\new.mp4");
//if (!file.exists()){
// file.createNewFile();
//}
//OutputStream outputStream = new FileOutputStream("E:\\CloudMusic\\MV\\new.mp4");
//long start = System.currentTimeMillis();
//int b;
//while ((b = inputStream.read()) != -1){
// outputStream.write(b);
//}
//long end = System.currentTimeMillis();
//System.out.println("单字节搬运:+" + (end - start));
InputStream inputStream = new FileInputStream("E:\\CloudMusic\\MV\\焦迈奇 - 我的名字.mp4");
File file = new File("E:\\CloudMusic\\MV\\new.mp4");
if (!file.exists()){
file.createNewFile();
}
OutputStream outputStream = new FileOutputStream("E:\\CloudMusic\\MV\\new.mp4");
long start = System.currentTimeMillis();
byte[] bytes = new byte[1024*1024];//将每次运货箱的大小提升;这样只需要进行比较少的i/o操作就可以完成;
// 如果还想优化新能,那么可以使用多线程;或者使用nio
int len;
while ((len = inputStream.read(bytes)) != -1){
outputStream.write(bytes,0,len);
}
long end = System.currentTimeMillis();
System.out.println("单字节搬运:" + (end - start) );
//上面的io是通过一个字节一个字节的读取文件,即使是8m左右大小的文,耗时大概是60分钟,可见io的效率不高;
//简单的优化
}
public String findMvFiles(String path){
IOTest ioTest = new IOTest();
File[] files = new File(path).listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return new File(dir, name).isDirectory() || name.contains("焦迈奇 - 我的名字.mp4");
}
});
if (files == null || files.length == 0){
return null;
}
for (File file : files) {
if (file.isDirectory()){
findMvFiles(file.getAbsolutePath());
//如果是文件夹,那么递归查找文件
} else{
System.out.println(file.getName());
ioTest.setTarPath(file.getAbsolutePath());
}
}
System.out.println(ioTest.getTarPath());
return ioTest.getTarPath();
}
}