2013年10月30日星期三

FileOutputStream automatically overwrite files problem

The following code does not output any data , because in bufferedReader before reading text messages
FileOutputStream have D: \ Debug.log cover empty file .. did not call the write method ..
delete FileOutputStream will be able to read normal ... this is why
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异常!");
}
}
}
}

------ Solution ------------------------------------- -------
you think this is why ah ? Why do they read and write , or the same file. What is your purpose ?
------ Solution ---------------------------------------- ----
new FileOutputStream (hosts); into new FileOutputStream (hosts, true); indicate additional documents
------ Solution --- -----------------------------------------
as upstairs ,
os = new FileOutputStream (hosts); into
os = new FileOutputStream (hosts, true); enough
As for the reason , look at the source


  public FileOutputStream(String name) throws FileNotFoundException {
        this(name != null ? new File(name) : null, false);
    }

continue to track

 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);
    }

find the open method

 /**
     * 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;

stopped here . Here, let us re-write or input parameter is added .
Since the input , and I think this should be the open method of preparation according to the input to work, such as re-write it emptied. .
------ For reference only -------------------------------------- -



append files can not read information
------ For reference only --------------------------- ------------

  
     
append files can not read information  
not think so . .
I read that you first determine whether that file under your content. . After all, you emptied it several times .

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,true);
            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异常!");
            }
        }
    }

------ For reference only ----------------------------------- ----
Thank you , I understand , OUTPUTSTREAM if not write also writes an empty file. This file can only be modified by overwriting the way

没有评论:

发表评论