上机模拟卷1A题

字符流

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

public class Main {
	public static void main(String[] args) throws IOException {
		String path="C:\\2021JAVA\\上机考试模拟题1\\上机考试模拟题1\\A卷(考生姓名)";
		FileInputStream in=new FileInputStream(path+"\\DataA.txt");
		
		int temp=in.read();// read()的理解:返回读入的一个字符,如果达到文件末尾,返回-1

		
		int count=0;
		while(temp!=-1)
		{   if(temp==124)//符号|的ASCII码是124
			count++;
			temp=in.read();
		}
		in.close();
		
		System.out.println(++count);
		
		
		File f=new File(path+"\\"+count+".txt");
		f.createNewFile();//新建n.txt文件
		
		FileInputStream in1=new FileInputStream(path+"\\DataA.txt");
		FileOutputStream out=new FileOutputStream(path+"\\"+count+".txt",true);		//文件字节输出流,输出到文件的数据追加到文件尾
		
		 OutputStreamWriter ouw=new OutputStreamWriter(out);//文件字符输出流
		
		ouw.write(""+count+"\r\n");
		
		temp=in1.read();
		
		
		while(temp!=-1)
		{  
			
			if(temp!=124)
			{
				ouw.write(temp);
			}
			else 
			{
				
				ouw.write(13);
			}
			temp=in1.read();
		}
		ouw.close();
		out.close();
		in1.close();
		
		
		
	}
}

 

字节流

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

public class Main {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		
		String path="C:\\2021JAVA\\上机考试模拟题1\\上机考试模拟题1\\A卷(考生姓名)";
		FileInputStream in=new FileInputStream(path+"\\DataA.txt");
		
		int temp=in.read();
		
		int count=0;
		while(temp!=-1)
		{   if(temp==124)
			count++;//符号|的ASCII码是124
			temp=in.read();
		}
		in.close();
		
		System.out.println(++count);
		
		
		File f=new File(path+"\\"+count+".txt");
		f.createNewFile();//新建一个n.txt文件
		
		FileInputStream in1=new FileInputStream(path+"\\DataA.txt");
		FileOutputStream out=new FileOutputStream(path+"\\"+count+".txt",true);		//文件字节输出流,输出到文件的数据追加到文件尾
		
		out.write((""+count+"\r\n").getBytes());//字节数组,存放了当前字符串中的所有字符
		
		temp=in1.read();
		
		
		while(temp!=-1)
		{  
			
			if(temp!=124)
			{
				out.write(temp);
			}
			else 
			{
				
				out.write(13);
			}
			temp=in1.read();
		}
		
		
	     
		out.close();
		in1.close();	
		
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

發財發財

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

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

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

打赏作者

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

抵扣说明:

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

余额充值