Java输入输出流的使用,对文件进行输入,输出,复制;

该代码展示了Java中如何进行文件读取、写入和复制操作。使用FileInputStream和FileOutputStream处理文件,通过read方法读取内容,write方法写入数据,并用genraterandom方法生成唯一的随机数写入文件。此外,还包含了异常处理确保资源的正确关闭。
摘要由CSDN通过智能技术生成
package 作业5;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.tree.FixedHeightLayoutCache;

public class IO {

	
	public static void readfile(File filename)
	{       FileInputStream fi=null;
		try {
			fi=new FileInputStream(filename);
			byte buffer[]=new byte[1024];
			int count;
			while((count=fi.read(buffer))!=-1)
			{  			
				System.out.print(new String(buffer,"GBK"));
			}
			System.out.println();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			if(fi != null)
			{
				try {
					fi.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	public static String genraterandom(int n)//n为要生成的随机数的个数
	{
		int a[]=new int[n];
		int num;
		for(int i=0;i<n;i++)
		{
			num=(int)(Math.random()*100);
			while(compare(a,num))
			{
			num=(int)(Math.random()*100);
			}
			a[i]=num;
		}
		ArrayList<String> str=new ArrayList();
		for(int i=0;i<a.length;i++)
		{
			str.add(Integer.toString(a[i]));
		}
		String s=str.toString();
		return s;
	}
	
public static void writefile(File filename,String str)
{ 
    	FileOutputStream fo=null;
	//byte[] buffer=new byte[1024];
	try {
		fo=new FileOutputStream(filename);
		fo.write(str.getBytes());
			
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		finally
		{
			if(fo != null)
			{
				try {
					fo.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	

}
public static void copyfile(File filename1,File filename2)
{
	  FileInputStream fi=null;
	  FileOutputStream fo=null;
	  try {
		fi=new FileInputStream(filename1);
		fo=new FileOutputStream(filename2);
		byte[]buffer=new byte[1024];
		int count;
		while((count=fi.read(buffer))!=-1)
		{
			fo.write(buffer);
			
		}
		
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  
}





private static boolean compare(int[] a, int num) {
	// TODO Auto-generated method stub
	for(int i=0;i<a.length;i++)
	{
		if(a[i]==num)
		return true;
	}
	
	return false;
}

public static void main(String[] args) {
	// TODO Auto-generated method stub
 File f=new File("C:/javatool/Mydream.txt");
 readfile(f);
File f1=new File("C:/javatool/Mydata.txt");
writefile(f1,genraterandom(10));
readfile(f1);
File f2=new File("C:/javatool/Mydata2.txt");
copyfile(f1,f2);
readfile(f2);

}
}

我接下来说一说此代码的难点

(count=fi.read(buffer))!=-1 是一个条件语句,它表示从 FileInputStream 对象 fi 中读取最多 buffer.length 个字节,并将它们存储在字节数组 buffer 中,同时返回读取的字节数。这个语句的作用是检查是否还有字节可供读取。如果读取到了文件的末尾,返回值就是 -1,循环就会结束。否则,返回值是实际读取的字节数,循环将继续执行。

差不多就这一个难读懂的地方吧;

还有就是此代码在对文件写入时要传入一个string类型的。

嗯嗯,差不多就到这了吧。

还有啥问题,可以私信我。

运行结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

monkey mam

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值