简体   繁体   中英

Why doesn't my code write to a text file?

I want to know why my code doesn't write to a text file, JVM doesn't throw any exceptions...

public class AlterData {

    Data[] information; 
    File informationFile = new File("/Users/RamanSB/Documents/JavaFiles/Information.txt");
    FileWriter fw;

    public void populateData(){
        information = new Data[3];

        information[0] = new Data("Big Chuckzino", "Custom House", 18);
        information[1] = new Data("Rodger Penrose", "14 Winston Lane", 19);
        information[2] = new Data("Jermaine Cole", "32 Forest Hill Drive", 30);
    }

    public void writeToFile(Data[] rawData){
        try{    
        fw = new FileWriter(informationFile);
        BufferedWriter bw = new BufferedWriter(fw);
        for(Data people : rawData){ 
            bw.write(people.getName()+ ", ");
            bw.write(people.getAddress() + ", ");
            bw.write(people.getAge() +", |");
            }
        }catch(IOException ex){
            ex.printStackTrace();
        }
    }

    public static void main(String[] args){
            AlterData a1 = new AlterData();
            a1.populateData();
            a1.writeToFile(a1.information); 
    }
}

You should call close or flush for the BufferedWriter instance in order to flush the data into the file. Anyway, it's always important to close any resource you are using.

Try calling bw.flush() after writing the data and then you have to close the stream with bw.close() after flushing.

When closing the BufferedWriter it would be best to put the close statement into a finally block to ensure that the stream is closed, no matter what.

You should also look into using a try with resource. This makes use of the AutoCloseable feature of BufferedWriter as follows:

try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File("path/to/file")))) {
     // Do something with writer      
} catch (IOException e) {
     e.printStackTrace();
}

That way java will make sure that the stream is closed for you when leaving the try body, no matter what happens.

You need to ensure that, we need flush() to push the changes to file, Also ensure that you are closing the File resources:

public void writeToFile(Data[] rawData){
        BufferedWriter bw = null;
        try{    
        fw = new FileWriter(informationFile);
        bw = new BufferedWriter(fw);
        for(Data people : rawData){ 
            bw.write(people.getName()+ ", ");
            bw.write(people.getAddress() + ", ");
            bw.write(people.getAge() +", |");
            }
        }catch(IOException ex){
            ex.printStackTrace();
        } finally {
               if(fw  != null) {
                     fw.close();
                     fw = null;
               }
               if(bw  != null) {
                     bw.close();
                     bw = null;
               }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM