1)读取文件
import java.io.*;
public class OpenFile{
/**
* 功能:Java读取txt文件的内容
* 步骤:1:先获得文件句柄
* 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
* 3:读取到输入流后,需要读取生成字节流
* 4:一行一行的输出。readline()。
* 备注:需要考虑的是异常情况
* @param filePath
*/
public static void readTxtFile(String filePath){
try {
String encoding="utf-8";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
public static void main(String argv[]){
String filePath = "D:\\1.txt";
// "res/";
readTxtFile(filePath);
}
}
import java.io.*;
public class Write1 {
public static void main(String args[]){
try{
System.out.print("Input:");
int count,n=512;
byte buffer[]=new byte[n];
count=System.in.read(buffer);//读取标准输入流
//创建文件流输出对象
FileOutputStream wf=new FileOutputStream("write.txt");
wf.write(buffer,0,count);//写入输出流
wf.close();//关闭输出流
System.out.println("Save to write.txt!");
}catch(IOException ioe){
System.out.print(ioe);
}catch(Exception e){
System.out.print(e);
}
}
}