4

This is my code sample. The code is pretty long just to test if a file is blank and then if it isn't, write onto it. Either way, the line if (!(data.equals("")) && !(data.equals(null))) doesn't work and even when the file is blank, it still goes through the Alert.

FileInputStream fIn = null;String data = null;InputStreamReader isr = null;
try{
    char[] inputBuffer = new char[1024];
    fIn = openFileInput("test.txt");
    isr = new InputStreamReader(fIn);
    isr.read(inputBuffer);
    data = new String(inputBuffer);
    isr.close();
    fIn.close();
}catch(IOException e){}

// this is the check for if the data inputted from the file is NOT blank
if (!(data.equals("")) && !(data.equals(null)))
{
    AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);
    builder.setMessage("Clear your file?" + '\n' + "This cannot be undone.")
    .setCancelable(false)
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            EditText we = (EditText)findViewById(R.id.txtWrite);
            FileOutputStream fOut = null;

            OutputStreamWriter osw = null;
            try{
                fOut = openFileOutput("test.txt", Context.MODE_PRIVATE);
                osw = new OutputStreamWriter(fOut);
                osw.write("");
                osw.close();
                fOut.close();
                we.setText("");
            }catch(Exception e){}
        }
    })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

Also, if anyone has a way of shorting up this code, I would be greatful!

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97

3 Answers3

16

If a file is blank (has no contents) its length is 0. The length returns also 0 if it doesn't exist; if this is a necessary distinction you can check if the file exists with the exists method.

File f = getFileStreamPath("test.txt");
if (f.length() == 0) {
    // empty or doesn't exist
} else {
    // exists and is not empty
}

The current approach fails to work because inputBuffer is an array of 1024 chars, and a strings created from it will also have 1024 chars, independently of how many chars were successfully read from the file.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • Are you sure the file really is empty? What is its length? – Joni Jul 26 '13 at 14:18
  • Ahh `openFileOutput` is an Android API function, I had assumed it was something you wrote yourself. It creates the file in a specific directory that you can obtain from `getFilesDir`, and there's a different API function to get the File object with the full path. Check the update. – Joni Jul 26 '13 at 14:35
  • `File f = new File(getFilesDir(), "test.txt");` works! and `File f = getFileStreamPath("test.txt");` works too – Michael Yaworski Jul 26 '13 at 14:38
2

Try this, good luck !

File sdcard = Environment.getExternalStorageDirectory();
        File f = new File(sdcard, "/yourfile");

if(!f.exsist()){
f.createNewFile();
//Use outwriter here, outputstream search how to write into a tet file in java code 
}
Tugrul
  • 1,760
  • 4
  • 24
  • 39
  • 1
    well I'm not trying to see if it exists, I'm trying to see if it's blank. Also, if you create the file `File f = new File(sdcard, "/yourfile");`, then wouldn't it always exist because you just created it? – Michael Yaworski Jul 26 '13 at 14:20
1

Since you're using openFileInput("test.txt") that returns FileInputStream, try

FileInputStream fIn = openFileInput("test.txt");
FileChannel channel = fIn.getChannel();

if(channel.size() == 0) {
  // This is empty
}
else {
  // Not empty
}

I don't have Java NIO experience.

Glenn
  • 12,741
  • 6
  • 47
  • 48