[英]Read file of objects and write it back to a new one a single character at a time
如何從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);
}
}
}
請參考以下代碼
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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.