栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java下模仿实现C#.NET里面的File类。

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java下模仿实现C#.NET里面的File类。

  最近用Java开发了一些项目,真心感觉Java的文件流真的没有C#.NET好用(说实话,我觉得Java就是没有C#好用)。所以,我用Java模仿了一下.NET里面的File类。

  先放一下.jar包的链接https://download.csdn.net/download/newtypeofc/85506712

  然后放一下源代码

目录

My_File_Controller接口

My_File_Controller_Class类

定义全局变量和构造函数

初始化窗口InitializeAllPath

AppendAllLines方法

AppendAllText方法

WriteAllText方法

ReadAllText方法

ReadAllBytes方法

WriteAllBytes方法

Create方法

Copy方法

Delete方法

Exists方法

Move方法

ReadAllLines方法

WriteAllLines方法


My_File_Controller接口
package My_File_Controller;

public interface My_File_Controller {
	public abstract String[] ReadAllLines(String path);
	public abstract void AppendAllLines(String path, String[] Text);
	public abstract void WriteAllLines(String path,String[] Text);

	public abstract String ReadAllText(String path);
	public abstract void AppendAllText(String path, String Text);
	public abstract void WriteAllText(String path, String Text);
	
	public abstract byte[] ReadAllBytes (String path);
	public abstract void WriteAllBytes(String path, byte[] Text);
	
	public abstract void Copy(String path1, String path2);
	public abstract void Create(String path);
	public abstract void Delete(String path);
	public abstract boolean Exists(String path);
	public abstract void Move(String path1, String path2);
}

My_File_Controller_Class类

定义全局变量和构造函数
package My_File_Controller;
import java.io.BufferedReader;

public class My_File_Controller_Class implements My_File_Controller {
	
	
	private String thisLine = new String();
	private FileWriter fileWriter = null;
	private FileReader fileReader = null;
	private BufferedReader bufferedReader = null;
	private BufferedWriter bufferedWriter = null;
	private FileInputStream fileInputStream = null;
	private FileOutputStream fileOutputStream = null;
	private DataInputStream dataInputStream = null;
	private File file = null;

	
	public My_File_Controller_Class() {
		InitializeAllPath();
}

初始化窗口InitializeAllPath
public void InitializeAllPath() {
		thisLine = new String();
		fileWriter = null;
		fileReader = null;
		bufferedReader = null;
		bufferedWriter = null;
		fileInputStream = null;
		fileOutputStream = null;
		dataInputStream = null;
		file = null;
}

AppendAllLines方法
public void AppendAllLines(String path, String[] Text) {
		try {
			fileWriter = new FileWriter(path);
			bufferedWriter = new BufferedWriter(fileWriter);
			String thisLine = new String();
			for(int i = 0;i < Text.length;i++) {
				thisLine = Text[i];
				bufferedWriter.write(thisLine + "rn");
			}
			bufferedWriter.flush();
			bufferedWriter.close();
		}
		catch(Exception ex) {
			
		}
}

AppendAllText方法
public void AppendAllText(String path, String Text) {
		try {
			//1.读取现有文件
			String Text_now = ReadAllText(path);
			
			//2.写入现有内容和新内容
			fileWriter = new FileWriter(path);
			bufferedWriter = new BufferedWriter(fileWriter);
			bufferedWriter.write(Text_now + Text);
			bufferedWriter.flush();
			bufferedWriter.close();
		}
		catch(Exception ex) {
			
		}
}

WriteAllText方法
public void WriteAllText(String path, String Text) {
		try {
			fileWriter = new FileWriter(path);
			bufferedWriter = new BufferedWriter(fileWriter);
			bufferedWriter.write(Text);
			bufferedWriter.flush();
			bufferedWriter.close();
		}
		catch(Exception ex) {
			
		}
}

ReadAllText方法
public String ReadAllText(String path) {
		String result = new String();
		try {
			System.out.println(path);
			fileReader = new FileReader(path);
			bufferedReader = new BufferedReader(fileReader);
			while((thisLine = bufferedReader.readLine()) != null) {
				result += thisLine;
			}
			bufferedWriter.flush();
			bufferedWriter.close();
		}
		catch(Exception ex) {
			
		}
		return result;
}

ReadAllBytes方法
public byte[] ReadAllBytes (String path) {
		byte[] result = new byte[100];
		try {
			fileInputStream = new FileInputStream(path);
			//dataInputStream = new DataInputStream(fileInputStream);
			fileInputStream.read(result,0,result.length);
			fileInputStream.close();
		}
		catch(Exception ex) {
			
		}
		return result;
}

WriteAllBytes方法
public void WriteAllBytes(String path, byte[] Text) {
		try {
			fileOutputStream = new FileOutputStream(path);
			fileOutputStream.write(Text);
			fileOutputStream.flush();
			fileOutputStream.close();
		}
		catch(Exception ex) {
			
		}
}

Create方法
public void Create(String path) {
		try {
			file = new File(path);
			file.createNewFile();
			file = null;
		}
		catch(Exception ex) {
			
		}
}

Copy方法
public void Copy(String path1, String path2) {
		try {
			byte[] text = ReadAllBytes(path1);
			//创建新文件
			Create(path2);
			//开始写入
			WriteAllBytes(path2,text);
		}
		catch(Exception ex) {
			
		}
}

Delete方法
public void Delete(String path) {
		try {
			file = new File(path);
			file.delete();
			file = null;
		}
		catch(Exception ex) {
			
		}
}

Exists方法
	public boolean Exists(String path) {
		file = new File(path);
		boolean result = file.exists();
		file = null;
		return result;
	}

Move方法
public void Move(String path1, String path2) {
		try {
			//1.读取并删除旧文件
			byte[] text = ReadAllBytes(path1);
			Delete(path1);
			//2.创建新文件
			Create(path2);
			//3.写入新文件
			WriteAllBytes(path2,text);
		}
		catch(Exception ex) {
			
		}
}

ReadAllLines方法
	public String[] ReadAllLines(String path) {
		file = new File(path);
		String[] result = new String[(int)file.length()];
		int i = 0;
		try {
			fileReader = new FileReader(path);
			bufferedReader = new BufferedReader(fileReader);
			while( (thisLine = bufferedReader.readLine()) != null ) {
				result[i] = thisLine;
				i++;
			}
			bufferedReader.close();
			
		}
		catch(Exception ex) {
			
		}
		return result;
	}

WriteAllLines方法
	public void WriteAllLines(String path,String[] Text) {
		try {
			System.out.println(Text);
			fileWriter = new FileWriter(path);
			System.out.println(path);
			bufferedWriter = new BufferedWriter(fileWriter);
			for(int i = 0;i < Text.length;i++) {
				bufferedWriter.write(Text[i] + "rn");
				System.out.println(Text[i].toString());
			}	
			bufferedWriter.flush();
			fileWriter.close();
			bufferedWriter.close();
		}
		catch(Exception ex) {
			
		}
	}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939277.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号