Search code examples
androidfilefileinputstreamfileoutputstream

create a file and take out specific data


I am trying to create a file and save data to it and then I want to be able to read it and if its possible to take out specific data.

So here is my code to create and open a file.

public void createFile(View view) throws IOException {
    String FILENAME = "hello_file";
    String string = "hello world";
    FileOutputStream fos = openFileOutput(FILENAME,Context.MODE_APPEND);
    fos.write(string.getBytes());
    fos.close();
}

public void openFile(View view) throws IOException{
    String FILENAME = "hello_file";
    FileInputStream g = openFileInput(FILENAME);
    System.out.println(g.read());
    g.close();
}

Since the read() is returning an int and the value is a String that is stored one bit at a time if I understood correct.

Should parsing be an option?


Solution

  • I currently use these two methods to read and write to a file in android :

    public String readFromInternalFile(String fileName) throws IOException {
        InputStream fis = context.openFileInput(fileName);
        ObjectInputStream ois = new ObjectInputStream(fis);
    
        String readData = ois.readObject().toString();
    
    
        return readData;
    }
    
    
    
    public void writeToInternalFile( String text, String fileName) {
        try  {
            OutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(text);
            fos.close();
            oos.close();
        }
        catch (Exception e){
            e.printStackTrace();
    
        }
    }
    

    the second method will write your object to an internal file so not on the external storage and the first one is too read the object. Here I am reading the object as a String but this of course depends on the type of object you wrote to the file. You can replace the return method of the readFromInternalFile and make it Object and change the argument of the writeToInternalFile from String text to Object objectToWrite so you can easily read and write any object to a file with the same method.

    So for universal read and write methods you will have :

    public Object readFromInternalFile(String fileName) throws IOException {
        InputStream fis = context.openFileInput(fileName);
        ObjectInputStream ois = new ObjectInputStream(fis);
    
        Object readData = ois.readObject();
    
    
        return readData;
    }
    
    
    
    public void writeToInternalFile( Object objectToWrite, String fileName) {
        try  {
            OutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(objectToWrite);
            fos.close();
            oos.close();
        }
        catch (Exception e){
            e.printStackTrace();
    
        }
    }