ファイルから文字列を読み取りたい。特定の文字列 ( ><
) が見つかったら、代わりに整数の読み取りを開始し、それらをバイナリ文字列に変換します。
私のプログラムは文字列を正常に読み取って保存していますがArrayList
、シンボルを認識しない><
ため、バイナリ文字列の読み取りは成功しません。
コード
try {
FileInputStream fstream = new FileInputStream(fc.getSelectedFile().getPath());
// Get the object of DataInputStream
DataInputStream ino = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(ino));
String ln;
String str, next;
int line, c =0;
while ((ln = br.readLine()) != null) {
character = ln;
System.out.println(character);
iname.add(ln); // arraylist that holds the strings
if (iname.get(c).equals("><")) {
break; // break and moves
// on with the following while loop to start reading binary strings instead.
}
c++;
}
String s = "";
// System.out.println("SEQUENCE of bytes");
while ((line = ino.read()) != -1) {
String temp = Integer.toString(line, 2);
arrayl.add(temp);
System.out.println("telise? oxii");
System.out.println(line);
}
ino.close();
} catch (Exception exc) { }
読み込もうとしているファイルは、たとえば次のとおりです。
T
E
a
v
X
L
A
.
x
"><"
sequence of bytes.
最後の部分がバイトとして保存され、テキストファイルにそのように表示されます。このビットが機能する心配はありません。すべての文字列が新しい行に保存されます。