日期:2014-05-20 浏览次数:21008 次
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.swing.JOptionPane;
public class _Writer
{
/**
* @param args
*/
public static void main(String[] args)
{
File hosts = new File("D:\\Debug.log");
FileOutputStream os = null;
FileInputStream is = null;
BufferedReader br = null;
BufferedWriter bw = null;
try
{
is = new FileInputStream(hosts);
os = new FileOutputStream(hosts);
br = new BufferedReader(new InputStreamReader(is));
bw = new BufferedWriter(new OutputStreamWriter(os));
String str = null;
while ((str = br.readLine()) != null)
{
System.out.println(str);
}
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
JOptionPane.showMessageDialog(null, "hosts文件不存在!");
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
JOptionPane.showMessageDialog(null, "IO异常!");
} finally
{
try
{
if (os != null)
os.close();
if (is != null)
is.close();
if (br != null)
br.close();
if (bw != null)
bw.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
JOptionPane.showMessageDialog(null, "CLOSE异常!");
}
}
}
}
public FileOutputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null, false);
}
public FileOutputStream(File file, boolean append)
throws FileNotFoundException
{
String name = (file != null ? file.getPath() : null);
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(name);
}
if (name == null) {
throw new NullPointerException();
}
this.fd = new FileDescriptor();
this.append = append;
fd.incrementAndGetUseCount();
open(name, append);
}
/**
* Opens a file, with the specified name, for overwriting or appending.
* @param name name of file to be opened
* @param append whether the file is to be opened in append mode
*/
private native void open(String name, boolean append)
throws FileNotFoundException;