ANDROID Input / Output FileStream
I'm having difficulty writing strings to and from files. Just your basic, internally created text files. For whatever reason, the IO code I've used with Java console programming doesn't seem to work, and the documentation on Android's I/O Streams for string files is skimpy at best.
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;
char[]inputBuffer = new char[1024];
fln = openFileInput("user1Profile.txt");
isr = new InputStreamReader(fln); 开发者_JAVA百科
isr.read(inputBuffer);
String_4 = new String(inputBuffer);
isr.close();
fln.close();
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 combination.You can use:
BufferedReader buf = new BufferedReader(new FileReader("user1Profile.txt"));
Then use:
buf.readLine();
to read each line in the text file yo your variables.
I can think of two solutions:
The read function could have the following header:
public int read (char[] buffer, int offset, int length)
You may specify how much characters you want to read in the length int. But you will have to store the length of the strings previously.
The read function without parameters will read a single char. You may choose some character (like
\n
) to mark the end of the string. So loop through read until you find the marker character to fill one string.
I can't test if this will work, but it's the way I would have done it. Hope this helps.
精彩评论