关于IO的6个课上小实验

实验1

package testIo;
//在D盘目录下创建HelloWorld.java
//使用字节流的方式进行文件的读取并打印至控制台
//计算所读取到的字节数,并在控制台打印




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


public class test1 {


public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File f=new File("D://helloworld.java");
FileInputStream fi=new FileInputStream("D://helloworld.java");
int i=1;
while((i=fi.read())!=-1){
System.out.print((char)i);
}

System.out.println("读取到的字节数为:"+f.length());
fi.close();
}
  
}



实验2


package testIo;


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
//将D盘中的HelloWorld.java文件使用字节流的方式复制到
//D盘下HW.java文件




public class test2 {
public static void main(String[] args) throws IOException {
FileInputStream f1=new FileInputStream("D://helloworld.java");
FileOutputStream f2=new FileOutputStream("D://HW.java");
int i;
while((i=f1.read())!=-1){
f2.write(i);
}
f1.close();
f2.close();
}


}




实验3

package testIo;


import java.io.FileReader;
import java.io.IOException;


//使用字符流的方式进行文件HelloWorld.java的读取并打印至控制台




public class test3 {
public static void main(String[] args) throws IOException {
FileReader f1=new FileReader("D://helloworld.java");
int i;
while((i=f1.read())!=-1){
System.out.print((char)i);
}
f1.close();

}


}

实验4

package testIo;


import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


//将D盘中的HelloWorld.java文件使用字符流的方式复制到D盘下HelloWorld.bak文件
//异常处理使用throws关键字




public class test4 {
public static void main(String[] args) throws IOException  {
FileReader f1=new FileReader("D://helloworld.java");
FileWriter f2= new FileWriter("D://helloworld.bak");
int i;
while((i=f1.read())!=-1){
f2.write(i);
}
f1.close();
f2.close();
}


}


实验5

package testIo;



import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


//使用FileReader类、BufferedReader类、FileInputStream类三种方式
//从D盘下HelloWorld.java文件中读取全部信息并打印到控制台
//异常处理使用throw关键字对可能的异常进行处理,细化异常的类型




public class test5 {
public static void main(String[] args) throws FileNotFoundException,IOException {
FileReader a=new FileReader("D:\\Hello World.java");
BufferedReader b=new BufferedReader(new FileReader("D:\\Hello World.java"));
FileInputStream c=new FileInputStream("D:\\Hello World.java");
int i;
while((i=a.read())!=-1){
System.out.print((char)i);
}
while((i=b.read())!=-1){
System.out.print((char)i);
}
a.close();
b.close();
while((i=c.read())!=-1){
System.out.print((char)i);
}
c.close();
}


}




实验6


package testIo;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;


//使用PrintWriter类、BufferedWriter类、FileOutputStream类三种方式
//用这三种方式分别将字符串“ABCDEFG”写入到D盘下HelloWorld1.txt、 HelloWorld2.txt、 HelloWorld3.txt文件中
//异常处理使用throws关键字,细化异常的类型






public class test6 {
public static void main(String[] args) throws FileNotFoundException ,IOException {
File file1=new File("D:\\","HelloWorld1.txt");
File file2=new File("D:\\","HelloWorld2.txt");
File file3=new File("D:\\","HelloWorld3.txt");
file1.createNewFile();
file2.createNewFile();
file3.createNewFile();
// InputStreamReader q=new InputStreamReader(System.in);
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
PrintWriter a=new PrintWriter(new FileOutputStream("D:\\HelloWorld1.txt"));
BufferedWriter b=new BufferedWriter(a);
FileOutputStream c=new FileOutputStream(("D:\\HelloWorld1.txt"));
String inputString=sc.next();
a.print(inputString);
a.close();
b.write(inputString);
b.close();
// String x="ABCDEFG";
c.write(inputString.getBytes());
c.close();
System.out.println();
// while((i=q.read())!=-1){
// System.out.print((char)i);
// a.print("1");
// }

// in.close();
// while((i=q.read())!=-1){
// b.write((char)i);
// }
// b.close();
// while((i=inputString.read())!=-1){
// c.write((char)i);
// }
// c.close();
// }


}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值