如何从txt文件中读取整个记录,分别获取每个字段并将每个字段转换为单独的字符流。然后将单个字符的字符流(在循环中)写入纯ASCII输出文本文件。
我有我的类定义,我似乎无法正确编写输出文件,它必须是一次一个单独的简单ascii文本字符。我只需要一些帮助。以下是我到目前为止的情况:
-----这是我的第一个问题。很抱歉,如果格式不正确:(我正在尝试将对象文件转换为简单的 ASCII字符文本文件,我称之为“yankees.txt”我用ObjectInputStream
读取它然后我应该分别得到每个字段并将每个字段转换成一个单独的字符流,并将每个字符一次从每个字段写入我的“yankees”。 TXT“
public class yankeesfilemain {
public static void main(String[] args) throws EOFException {
ObjectInputStream is;
OutputStream os;
yankees y;
int i, j, k;
String name, pos;
int number;
File fout;
try {
is = new ObjectInputStream(new
FileInputStream("yankees.yanks"));
y = (yankees)is.readObject();
fout = new File("yankees.txt");
os = new FileOutputStream(fout);
while (y != null) {
name = y.getname();
pos = y.getpos();
number = y.getnum();
for (i = 0; i < .length(); i++) {}
for (j = 0; j < .length(); j++) {
pos = y.getpos();
}
for (k = 0; k < .length(); k++) {
number = y.getnum();
}
break;
}
os.close();
is.close();
} catch(EOFException eof) {
eof.printStackTrace();
System.exit(0);
} catch(NullPointerException npe) {
npe.printStackTrace();
System.exit(0);
} catch(NumberFormatException nfe) {
nfe.printStackTrace();
System.exit(0);
} catch(IOException e) {
e.printStackTrace();
System.exit(0);
}
}
}
答案 0 :(得分:0)
请参阅以下代码
public static void main(String[] args) throws IOException {
InputStream in = new FileInputStream("C:\\11.txt");
OutputStream out = new FileOutputStream("C:\\12.txt", true);
try {
byte[] buffer = new byte[1024];
while (true) {
int byteRead = in.read(buffer);
if (byteRead == -1)
break;
out.write(buffer, 0, byteRead);
}
}
catch (MalformedURLException ex) {
System.err.println(args[0] + " is not a URL Java understands.");
} finally {
if (in != null)
in.close();
if (out != null) {
out.close();
}
}
}