当前位置: 代码迷 >> J2SE >> ,java如何用文件
  详细解决方案

,java如何用文件

热度:17   发布时间:2016-04-23 20:39:40.0
求助,java怎么用文件
就是做一个日历记事本,可以储存日志,具体怎么写啊
------解决方案--------------------
上面的处理中文会乱码,用这个吧。


package FileOp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JOptionPane;

public class FileDemo {
public static void main(String[] args) {

///////读文件
File file = new File("e:/a.txt");
FileInputStream inputfile = null;
StringBuffer str= new StringBuffer(" ");
try{
FileReader readIn = new FileReader(file);
int size = (int)file.length();
int charsRead=0;
char content[] = new char[size];
while(readIn.ready())
charsRead +=readIn.read(content, charsRead, size-charsRead);
readIn.close();
System.out.print(new String(content, 0, charsRead));
try{
readIn.close();
}catch(IOException e) {
e.printStackTrace();
}
}catch(IOException exx) {
exx.printStackTrace();
}


///写文件
File fOut = new File("e:/b.txt");
FileOutputStream outStream=null;
try{
outStream = new FileOutputStream(fOut);
}catch(FileNotFoundException fe) {
fe.printStackTrace();
}

try{
outStream.write("abcdefg".getBytes());
}catch(IOException ioe) {
ioe.printStackTrace();
}
try{
outStream.close();
}catch(IOException e){
e.printStackTrace();
}
}
}

  相关解决方案