rex
rex

Reputation: 821

is this a most effective way to store and read some text data?

i am thinking about there are some many way to store data in file , i found one of this is useing buffedinputstream ,but i really don't know is it good ?? if i using like this , it will be most fast ?? is there any other suggestion ?? i just want make the file io more fast !!

 public ArrayList<String> testReadingTxtFromFile(){
        ArrayList<String> result = null;
        try {
         FileInputStream fIn = openFileInput("cacheingtext.txt");
         InputStreamReader isr = new InputStreamReader(fIn);
         BufferedReader reader = new BufferedReader(isr);
         String line;
         while((line = reader.readLine() )!= null){
             String[] datas = line.split(",");
             Log.i("check", datas.length+"");
             for(String data:datas){
                 Log.i("check", data);
                 result.add(data);
             }
         }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }


public void testWritingTxtToFile(String[] messages){
    try {
        FileOutputStream fo = openFileOutput("cacheingtext.txt", MODE_WORLD_READABLE);
        OutputStreamWriter osw = new OutputStreamWriter(fo); 
        BufferedWriter writer = new BufferedWriter(osw);
        int size = messages.length;
        for(int i=0;i<size;i++){
            writer.write(messages[i]);
            writer.write(",");
        Log.i("check", "write "+messages[i]);
        }
        writer.flush();
        writer.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

Upvotes: 0

Views: 179

Answers (1)

Shashank Kadne
Shashank Kadne

Reputation: 8101

The Reader/Writer class hierarchy is character-oriented, and the Input Stream/Output Stream class hierarchy is byte-oriented. Basically there are two types of streams.Byte streams that are used to handle stream of bytes and character streams for handling streams of characters.

What I see in your case is that you are using a byte-oriented Stream.

Character streams are often "wrappers" for byte streams. The character stream uses the byte stream to perform the physical I/O, while the character stream handles translation between characters and bytes. FileReader, for example, uses FileInputStream, while FileWriter uses FileOutputStream.

So,if you want to generally deal with Characters (reading text files), go for Character-oriented Stream(Reader/Writer). But if you want to process the content independent of what type of file is it, go for byte-oriented stream.

Upvotes: 1

Related Questions