Java基础功能实例——IO流基础实例

本文主要包含了JAVA IO流的基础小实例

第一个是IO流中的输入流(inputstream)——将文件里的数据按照行来获取

package com.FileIO.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class Fileinput {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

		File f = new File("C:/Test", "File_IO_test.txt");

		FileInputStream Fis = new FileInputStream(f);

		InputStreamReader Isr = new InputStreamReader(Fis);// 将字节流转换为字符流

		BufferedReader Br = new BufferedReader(Isr);// 通过行的方式读取字符流

		String line = null;

		while ((line = Br.readLine()) != null) {

			String str = line.substring(0, 1500);

		}

		Br.close();
		Isr.close();
		Fis.close();

	}

}

第二个是IO流中的输入流(inputstream)——将文件里的数据 按照字节来获取

package com.FileIO.test;


import java.io.File;
import java.io.FileInputStream;

public class Fileinput {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

		File f = new File("C:/Test", "File_IO_test.txt");

		FileInputStream Fis = new FileInputStream(f);

		byte[] buff = new byte[1557];

		int bytesRead = 0;

		while ((bytesRead = Fis.read(buff)) > 0) {

			System.out.println("获取到字节流长度为:"+bytesRead);
			String msg = new String(buff);			
		}

		Fis.close();

	}

}


第三个是IO流中的输出流(outputstream)——将数据写入到文本中


package com.FileIO.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

public class Fileoutput {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

		File f = new File("C:/Test", "File_IO_test.txt");

		FileOutputStream Fos = new FileOutputStream(f);

		OutputStreamWriter osw = new OutputStreamWriter(Fos, "GBK");

		String str = "this is a testing" + "\r";

		System.out.println(f.getName());

		osw.write(str);

		osw.close();

	}

}


第四个是最简单地将文件通过IO的方式进行复制:

package com.FileIO.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class CopyFile {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		FileInputStream Fis = null;
		FileOutputStream Fos = null;

		try {
			File src = new File("C:/Test", "CopyTest.txt");
			File des = new File("C:/Test", "CopyTest_1.txt");
			File dest = new File("C:/Test", "CopyTest_2.txt");

			// 加入了commons-io-2.4.jar的原因可以直接调用里面的方法
			FileUtils.copyFile(src, des);

			// 如果没有jar包只能通过IO的方式如下:

			Fis = new FileInputStream(src);

			Fos = new FileOutputStream(dest);

			int d = -1;

			while ((d = Fis.read()) != -1) { // 一个一个字节的读取Fis中字节流
				Fos.write(d);
				System.out.println(d);
				Fos.flush();
			}

			System.out.println("finish");

		} catch (FileNotFoundException e) {
			System.out.println("文件没有找到");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("数据读写异常");
			e.printStackTrace();

		} finally {
			if (Fis != null) {
				try {
					Fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		if (Fos != null) {
			try {
				Fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

}

















  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值