当前位置:   article > 正文

java中的文件操作:读取写入byte[]字节流、string字符串、list列表_读取文件流bytedata

读取文件流bytedata

全栈工程师开发手册 (作者:栾鹏)

java教程全解

java中文件操作:读取文件成字节流,将字节流写入文件,按行读取文件成字符串列表,将字符串列表存储成文件,读取文件成字符串,将字符串写入文件。

主程序测试文件

	//测试程序
	public static void main(String[] args) 
	{
				byte[] data=file2byte("test.txt");  				//读取文件
		byte2file("test1.txt",data);  						//写入文件
		ArrayList<String> allline=file2list("test.txt","UTF-8");  	//按行读取文件
		list2file("test2.txt",allline,"UTF-8");  					//存储文件
		String content=file2str("test.txt","UTF-8");  				//读取文件
		str2file("test3.txt",content,"UTF-8");  					//存储文件
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

读取文件成字节数组

	public static byte[] file2byte(String path)
	{
		try {
			FileInputStream in =new FileInputStream(new File(path));
			//当文件没有结束时,每次读取一个字节显示
			byte[] data=new byte[in.available()];
			in.read(data);
			in.close();
			return data;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

将字节数组写入文件

	public static void byte2file(String path,byte[] data) {
		try {
			FileOutputStream outputStream  =new FileOutputStream(new File(path));
			outputStream.write(data);
			outputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

按行读取文件成list

	public static ArrayList<String> file2list(String path,String encoder) {
		ArrayList<String> alline=new ArrayList<String>();
		try {
			BufferedReader in =new BufferedReader(new InputStreamReader(new FileInputStream(path),encoder));
			String str=new String();
			while ((str=in.readLine())!=null) {
				alline.add(str);
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return alline;
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

输出list到文件

	public static void list2file(String path,ArrayList<String> data,String encoder) 
	{
		try {
			BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),encoder));
			for (String str:data) {
				out.write(str);
				out.newLine();
			}
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

从标准输入中读入

	public static String system2str() throws IOException{
		BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
		return stdin.readLine();
	}
  • 1
  • 2
  • 3
  • 4

读取文件成字符串

	public static String file2str(String path,String encoder) 
	{
		StringBuilder sb=new StringBuilder();
		try {
			BufferedReader in =new BufferedReader(new InputStreamReader(new FileInputStream(path),encoder));
			String str=new String();
			while ((str=in.readLine())!=null) {
				sb.append(str);
			}
			in.close();	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

输出字符串到文件

	public static void str2file(String path,String data,String encoder) 
	{
		try {
			BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),encoder));
			out.write(data);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

读取文件成数据矩阵

	public static ArrayList<Double> file2matrix(String path) 
	{
		ArrayList<Double> alldata=new ArrayList<Double>();
		try {
			DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream(path)));
			//利用DataInputStream来读数据
			while(true)
			{
				alldata.add(in.readDouble());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return alldata;
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/453953
推荐阅读
相关标签
  

闽ICP备14008679号