file input/output stream (text file)

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

file input/output stream (text file)

Postby jrturner0112 » Tue Feb 22, 2011 9:16 pm

I'm having difficulty writing strings to and from files.

String String_1 = "I'm";
String String_2 = "Getting";
String String_3 = "Frustrated";
String String_4 = null;
String String_5 = null;
String String_6 = null;

FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try{
fOut = openFileOutput("user1Profile.txt", 0);
osw = new OutputStreamWriter(fOut);
osw.write(String_1);
osw.write(String_2);
osw.write(String_3);
fOut.close()
}
catch(){}
----------------------------------
FileInputStream fln = null;
InputStreamReader isr = null;

try {
char[]inputBuffer = new char[1024];
fln = openFileInput("user1Profile.txt");
isr = new InputStreamReader(fln);
isr.read(inputBuffer);
String_4 = new String(inputBuffer);
isr.close();
fln.close();
}

catch() {}

Will all 3 strings be stored in String_4?

How would I put String 1, 2, and 3 into 4, 5 and 6???
I've tried countless combinations. Please help.
jrturner0112
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Feb 22, 2011 9:11 pm

Top

Re: file input/output stream (text file)

Postby lockon_stratos » Wed Mar 16, 2011 7:14 am

hi jrturner0112,

Here is the code snippet on how to write and read using file stream.

Writing using FileOutputStream:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. try {
  2.                 String str1 = "hello";
  3.                 String str2 = "world";
  4.                
  5.                 FileOutputStream fos = openFileOutput("test.txt", MODE_PRIVATE);
  6.                 fos.write(str1.getBytes());
  7.                 fos.write(str2.getBytes());
  8.                 fos.close();
  9.         } catch(IOException ex) {
  10.                 Log.e(TAG, ex.getMessage());
  11.         }
  12.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Reading using FileInputStream:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. try {
  2.                                         FileInputStream fis = openFileInput("test.txt");
  3.                                         byte[] b = new byte[1024];
  4.                                         String s = "";
  5.                                        
  6.                                         while(fis.read(b) > -1) {
  7.                                                 s += new String(b, 0, b.length);
  8.                                         }
  9.                                        
  10.                                         fis.close();
  11.                                         TextView textview1 = (TextView) findViewById(R.id.textview1);
  12.                                         textview1.setText(s);
  13.                                 } catch(IOException ex) {
  14.                                         Log.e(TAG, ex.getMessage());
  15.                                 }
  16.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
Lets go to snipe!
User avatar
lockon_stratos
Freshman
Freshman
 
Posts: 7
Joined: Wed Mar 16, 2011 5:00 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 9 guests