//字节流in.txt文件的内容copy到 out.txt中
import java.io.*;
class Test{
public static void main(String args []){
//申明输入流引用
FileInputStream fis = null;
//申明输出流的引用
FileOutputStream fos = null;
try{
//生成代表输入流的对象
fis = new FileInputStream("e:/java/in.txt");
//生成代表输出流的对象
fos = new FileOutputStream("e:/java/to.txt");
//生成一个字节数组
byte [] buffer = new byte[100];
//调用输入流对象的read方法,读取数据
int temp = fis.read(buffer,0,buffrt.length);
//read方法会返回读了多少字节,
fos.write(buffer,0,temp);
for(int i = 0 ; i < buffrt.length ; i++){
String s = new String(buffrt);
//调用一个String对象的trim方法,将会去除掉这个字符串的首尾空格和空字符
s = s.trim();
System.out.println(s);
}
}
catch(Exception e){
System.out.println(e);
}
finally{
try{
fis.close();
fos.close();
}
Catch(Exception e){
System.out.println(e);
}
}
}
}
//字节流将比较大的in.txt文件的内容copy到 out.txt中
import java.io.*;
class Test{
public static void main(String args []){
//申明输入流引用
FileInputStream fis = null;
//申明输出流的引用
FileOutputStream fos = null;
try{
//生成代表输入流的对象
fis = new FileInputStream("e:/java/in.txt");
//生成代表输出流的对象
fos = new FileOutputStream("e:/java/to.txt");
//生成一个字节数组
byte [] buffer = new byte[1024];
while(true){
//调用输入流对象的read方法,读取数据
int temp = fis.read(buffer,0,buffrt.length);
if(temp == -1){
break;
}
fos.write(buffer,0,temp);
}
}
catch(Exception e){
System.out.println(e);
}
finally{
try{
fis.close();
fos.close();
}
Catch(Exception e){
System.out.println(e);
}
}
}
}
//字符流:读写文件时,以字符为基础
//字节输入流:Reader <-- FileReader
//int read(char[] c , int off , int len)
//字节输出流:Writer <-- FileWriter
//void write(char [] c ,int off , int len)
import java.io.*;
public class TestChar{
public static void main(String args[]){
FileReader fr = null;
FileWriter fw = null;
try{
fr = new FileReader("e:/java/in.txt");
fw = new FileWriter("e:/java/out.txt");
char [] buffer = new char[100];
int temp = fr.read(buffer,0,buffer.length);
fw.write(buffer,0,temp);
}
catch(Exceotion e){
System.out.println(e);
}
finally{
try{
fr.close();
fw.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
}
import java.io.*;
class Test{
public static void main(String args []){
FileReader fileReader = null;
BuffereReader bufferedReader = null;
try{
fileReader = new FileReader("e:java.in.txt");
bufferedReader = new BufferedReader(fileReader);
String line = null;
while(true){
line = bufferedReader.readLine();
if(line == null){
break;
}
System.out.println(line);
}
}
catch(Exception e){
System.out.println(e);
}
finally{
try{
bufferedReader.close();
fileReader.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
}
javaIO
最新推荐文章于 2022-05-10 18:15:25 发布