JAVA 对文件的基本操作

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


/**
 * 文件输入输出流的基本操作 
 * @author 挚友L
 *
 */
public class Main {

private static final String pathname = "D:\\Java\\java-code\\Main\\src\\Main.java";
private static BufferedOutputStream bufferedOutputStream;
private static BufferedInputStream bufferedInputStream;

public static void main(String[] args) throws IOException{
//ReadFile();
ReadFileIO(pathname);
//WriteFileIO("D:\\电影\\test.mp4");
//WriteFile();
//DeleteFile("D:\\\\电影\\\\test.mp4");
}

/*文件写方法*/
public static void WriteFile() throws IOException {
final String FileName1 = "D:\\Java\\java-code\\Main\\src\\This_is_a_test.java";
//true追加写,false覆盖写
OutputStream outputStream = new FileOutputStream(FileName1, true); 
//写的内容
String content = "123";
//写操作时转换成byte型数据
outputStream.write(content.getBytes());
//完成操作后要关闭
outputStream.close();
}

/*用流进行写操作*/
public static void WriteFileIO(String pathname) throws IOException {
System.out.println("正在写入,请稍等……");
String pathnameIn = "D:\\电影\\飞鸟娱乐[bbs.hdbird.com].逍遥法外.720P 中英字幕\\MyDownloads\\fc2-ppv-597145-1.mp4";
long timestart = System.currentTimeMillis(); //开始的时间

//读要写入的文件
FileInputStream fileInputStream = new FileInputStream(pathnameIn);
bufferedInputStream = new BufferedInputStream(fileInputStream);
double size = (double)bufferedInputStream.available()/1024; //文件大小
byte[] bytes = new byte[bufferedInputStream.available()];
bufferedInputStream.read(bytes);

//写文件
FileOutputStream fileOutputStream = new FileOutputStream(pathname);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
bufferedOutputStream.write(bytes);

//先关缓存,再关文件流
bufferedInputStream.close();
bufferedOutputStream.close();
fileInputStream.close();
fileOutputStream.close();

long timeend = System.currentTimeMillis(); //结束时间
System.out.println("写入操作完成!\n写入文件大小" + size + "KB\n"
+ "共耗时" + (timeend-timestart) + "ms");
}


/*文件读方法*/
public static void ReadFile() throws IOException {


File file = new File(pathname);
System.out.println(file.getName());
InputStream inputStream = new FileInputStream(file);

/*
* 读取文件的基本操作
* 在读取文件时可以对byte[] 进行编码(utf-8或其它)加密
*/
// available() 获取当前文件大小(预估大小, 在电脑上可以操作,但在网络请求时得不到其正确大小)
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);

//基本操作
// int count = 0;
// while((bytes[count] = (byte)inputStream.read()) != -1) {
// count ++;
// }

//将读取的字符转换成可打印的字符串
String content = new String(bytes);
System.out.println(content);
//完成操作后要关闭
inputStream.close();
}


/*用流进行读操作*/
public static void ReadFileIO(String pathname) throws IOException {

BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(pathname));
byte[] bytes = new byte[bufferedInputStream.available()];
bufferedInputStream.read(bytes);
System.out.println(new String(bytes));
}

/*删除文件操作*/
public static void DeleteFile(String pathname) {
File file = new File(pathname);
if(file.exists()) {
System.out.println("文件:" + file.getName());
System.out.println("路径:" + file.getAbsolutePath());
file.delete();
System.out.println("状态:删除成功!");
}else {
System.err.println("文件不存在!");
}
}
}

猜你喜欢

转载自blog.csdn.net/A_Thinking_Reed_/article/details/80641075