java输入与输出

首先理解几个概念
1、字节文件:一个个字节组成,由数值在内存中的编码表示。
2、文本文件(本质上也是字节文件):也是一个个字节组,由数值字符表示。
思考:120 的表示
字节文件:四个字节,120的补码
文本文件:120,三个字节,“1”,“2”,“0”
字节流:
FileInputStream:字节文件的读
FileOutputStream:字节文件的写
文件输入流
举例:

//将D:\test.txt(不含汉字)内容在屏幕上打印
import java.io.*;
public class test{
	public static void main(String []args)
	throws Exception
	{
	FileInputStream fis=new FileInputStream("d:/test.txt");
	int c;
	while((c=fis.read())!=-1)
	{
	System.out.print(""+(char)c);
	}
	fis.close();
	}
}
//任意文件的复制
//只能是字节流来做,不能是字符流——因为字符流内部存在编码转换
FileInputStream fis1=new FileInputStream("d:/bk.jpg");
FileOutputStream fos=new FileOutputStream("w:/bk.jpg");//此时,文件已经创建但内容为空而已。
int ch;//byte[] ch=new byte[1024*1024];缓冲区技术,缩短时间
//int n
while((ch=fis1.read())!=-1)//while((n=fis1.read(ch))!=-1)//尽可能将ch读满
{
fos.write(ch);//fos.write(ch,0,n);
}
fis1.close();
fos.close();

按java基本数据类型从字节文件中读和写
举例:现有字节文件存储50个整数,读出50个整数并求和与平均值
分析:如果调用FileInputStream的read()方法,每次仅能读一个字节,而我们需要4个字节组成的整数值,并不是一个字节的补码组成的四个字节。所以我们需要对每4个字节进行处理,还原成整数值。所以这里使用新的两个类。
DataInputStream:包含了FileInputStream所有功能

DataInputStream(InputStream in)//构造器,只要是InputStream的子类都可以作为输入
readxxx()//xxx指java基本数据类型
public final int readInt() throws IOException//内部调用read()读取四次,再根据补码拼装成int返回
double readDouble()//内部调用read()读取八次,再根据补码拼装成double返回

readInt():如何判断结束
如果读到文件结束,将抛出EOFException

DataInputStream dis=new DataInputStream(
new FileInputStream("data.dat")
);
int v;
try{
while(true){
	v=dis.readInt();
	//对v进行处理
}
}
catch(EOFException e){}
dis.close();

DataOutputStream

DataOutputStream dos=new DataOutputStream(
new FileOutputStream("d:/data.dat")
);
for(int i=1;i<=50;i++)
	dos.writeInt(i);
dos.flush();
dos.close();

字符流(文本流)
文本文件读:
FileReader,以字符为单位
文本文件写:
FileWriter

//文本文件以字符为单位读,d盘下有文本文件t
FileReader fr=new FileReader("d:/t.txt");
int ch1;
while((ch1=fr.read())!=-1){
	System.out.print(" "+(char)ch1);
}
fr.close()
//文本文件写,以字符为单位
FileWriter fw=new FileWrite("d:/t.txt");
String sttr="Hello java";
fw.write(str);
fw.flush();
fw.close();

以文本行为单位的读
BufferReader:包含了FileReader的所有功能

String readLine();//读文本行
//以文本行为单位读取文件内容
BufferReader br=new BufferReader(
new FileReader("d:/t.txt")
);
//每次读取一行
String line=null;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();

文本文件有格式写
PrintWriter

//有格式的文本文件写
//99乘法表
PrintWriter pw=new PrintWriter("d:/t.txt");
for(int i=1;i<=9;i++){
	for(int j=1;j<=i;j++){
		pw.print(i+"*"+j+"="+i*j);
	}
	pw.println();
}
pw.flush();
pw.close();

文本文件读基本数据类型
Scanner(首选,可以取代BufferReader)

//Scanner用法1:从键盘上读入整数值
Scanner sc=new Scanner(System.in);
int v1=sc.nextInt();
//Scanner用法2:从文件中读取基本数据类型
Scanner sc= new Scanner(new File("d:/t.txt"));
while(sc.hasNextInt())//判断sc对象中是否还有下一个整数值
{
	int v=sc.nextInt();
}
//从串中读取
Scanner sc=new Scanner("    32    12    45");
whiile(sc.hasNextInt())
{
	int v=nextInt();
}

//Scanner处理文本行
public static void main(String []args) throws Exception{
Scanner sc=new Scanner(new File(d:/Input.txt));
while(sc.hasNextLine()){
	String line=sc.nextLine();
	System.out.println(line);
}
sc.close();
}
//Scanner基本数据类型的读
//有Input.txt
A001 王平 男 8500
A002 小红 女 9600
...
要求:输出Output.txt
男职工平均工资:...元
女职工平均工资:...//串——next();
//int——nextInt()
Scanner判结束——hasNext()
Scanner sc=new Scanner(new File(d:/Input.txt));
int s1=0,s2=0,c1=0,c2=0;//男工资总额,女工资总额,男人数总额,女人数总额
while(sc.hasNext()){
	String no=sc.next();
	String name=sc.next();
	String sex=sc.next();
	int salary=sc.nextInt();
	if("男".equalsIgnoreCase(sex))//忽略大小写
		{s1+=salary;
		c1++;}
		else
		{
			s2+=salary;
			c2++;
		}
}
PrintWriter pw=new PrintWriter(
new FileWriter("d:/Output.txt"));
pw.println("男职工平均工资:"+(c1==0?0:s1*1.0/c1)+"元");
pw.println("男职工平均工资:"+(c2==0?0:s2*1.0/c2)+"元");
pw.close();

如何告诉Scanner数据之间的分割方法

Scanner sc=new Scanner(new File(d:/t.txt));
//由逗号,空格,换行组成的分隔符
sc.useDelimiter("[\\s,\\n]+");//useDelimiter(Pattern)说明分割符是什么,形参是正则式
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

K_K_Chen

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

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

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

打赏作者

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

抵扣说明:

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

余额充值