Data not appearing in file written using HADOOP LocalFileSystem

Viewed 2797

I have written folowing code to write a couple of bytes to a local file using LocalFileSytem of HADOOP.i have used flush() , which as per my knowledge flushes JVM buffer and hsynch() flushes OS buffers leading to data being written to disk. But here in my case its data is not appearing in the file "1.txt" . But while i am closing the out put stream using close()[for the time being i have commented out in my code] data is perfectly appearing. Please rectify me if my understanding about flush() and hsynch() is correct or not ? If correct why data is not appearing ?

package hdfsTrying.hdfstrying;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import com.google.protobuf.ByteString.Output;

public class LocalFileAccess {
    public static void main(String arg[])
    {
        Path p = new Path("/home/priya/1.txt");
        FileSystem fs;
        Configuration cfg = new Configuration();
        try
        {
            fs= FileSystem.getLocal(cfg);

            FSDataOutputStream out = fs.create(p);
            out.write("Hi This should be written to file 1.txt".getBytes());
            out.flush();
            out.hsync();
            //out.close();
            FileStatus fst[]  = fs.listStatus(p);
            for(FileStatus g:fst)
             System.out.println(g.getPath());

        }
        catch(IOException io)
        {
            System.out.println("I am having exception");
            System.out.println(io.getMessage());
        }


    }

}
2 Answers
Related