欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第八章

程序员文章站 2022-07-12 17:58:42
...

学员操作:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import javax.print.DocFlavor.INPUT_STREAM;

public class file {
static String string=null;
public static void main(String[] args) {
OutputStream out=null;
File f=new File(“D:\我的青春谁做主.txt”);
try {
f.createNewFile();
out=new FileOutputStream(f,true);
String string=“aaa”;
byte[] bt=string.getBytes(“UTF-8”);
out.write(bt);

	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		if(out!=null){
			
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}
	
	readData(f);
	out();
}
public static void out(){
	
	OutputStream out1=null;
	File f1=new File("C:\\myFile\\我的青春谁做主.txt");
	
	try {
		f1.createNewFile();
		out1=new FileOutputStream(f1,true);
		
		byte[] bt=string.getBytes("UTF-8");
		out1.write(bt);
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		if(out1!=null){
			
			try {
				out1.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}
	
	
}

public static void readData(File f){
	InputStream in = null;
	try {
		in = new FileInputStream(f);
		byte [] buff = new byte[1024];
		int len = in.read(buff);
		System.out.println(len);
		String str = new String(buff,0,len);
		System.out.println(str);
		string=str;
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		if(in != null){
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

}

1.输入流与输出流。
2.编码,文件路径等一些错误
3.package jbit.io.homework;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReaderAndWriterFile {
public static void main(String[] args) {
BufferedReader reader=null;
BufferedWriter writer=null;
try {
//创建 FileReader对象和FileWriter对象.
FileReader fr = new FileReader(“c:\source.txt”);
FileWriter fw = new FileWriter(“d:\target.txt”);
//创建 输入、输入出流对象.
reader = new BufferedReader(fr);
writer = new BufferedWriter(fw);
String line = null;
StringBuffer sbf=new StringBuffer();
//循环读取并追加字符
while ((line = reader.readLine()) != null) {
sbf.append(line);
}
writer.write(sbf.toString()); //写入文件
} catch (FileNotFoundException e) {
System.out.println(“无此文件”);
}catch (IOException e) {
e.printStackTrace();
}finally{
//关闭 reader 和 writer.
try {
if(reader!=null)
reader.close();
if(writer!=null)
writer.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}

package jbit.io.homework;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Writer;

public class ReadIinFile {
public static void main(String[] args) {
FileInputStream fis = null;
DataInputStream dis = null;
try {
//创建输出流对象
fis = new FileInputStream(“C:\Windows\win.ini”);
dis = new DataInputStream(fis);
//创建输入流对象
int temp;
//读取文件并写入文件
while ( (temp = dis.read()) != -1) {
System.out.print((char)temp);

      }
    }catch (FileNotFoundException e) {
    	System.out.println("无此文件");
    }catch (IOException ioe) {
      ioe.printStackTrace();
    }finally{
    	try {
    		if(fis!=null)
    			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
}

}

上一篇: 第八章

下一篇: STL——算法