我在eclipse中編寫了一個java程序,將文件作爲輸入並輸出一個新文件作爲輸出,遵循一些邏輯。DataOutputStream.write(int b)不寫
我使用datainputstream輸入文件和輸出文件的dataoutputstream。 我使用那些readln()方法,效果很好。
現在我有一個write()方法的問題,只是它不寫任何東西! 我已經tryed randomAccess輸入,和/或bufferedoutputstream的輸出。
我已經tryed在一個單獨的Java項目這樣的:
import java.io.*;
import javax.swing.*;
public class apri_file {
@SuppressWarnings("deprecation")
public static void main(String[] args){
File inputFile = null;
File outputFile = null;
String dati;
int dato;
try {
JFileChooser FileWindow = new JFileChooser();
FileWindow.isDirectorySelectionEnabled();
FileWindow.setApproveButtonText("Open IPL File");
if(FileWindow.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
inputFile = FileWindow.getSelectedFile();
System.out.println("input path: " + inputFile.getAbsolutePath());
}
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(inputFile)));
System.out.println("input buffer reader created");
/**************************/
FileWindow.setApproveButtonText("Save PLY FIle");
if(FileWindow.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){
outputFile = new File (FileWindow.getSelectedFile(),"");
System.out.println("output path: " + outputFile.getAbsolutePath());
}
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream (outputFile)));
/**************************/
/*
* to do write header
*/
for(int i=0;i<inputFile.length();i++){
dati = in.readLine();
if(dati == null){
break;
}
if(dati == "vnod"){
while(in.read() != 0X65){
dato = in.read();
if(dato == 0X09 || dato == 0X0D || dato == 0X2C){ }
else if(dato == 0X2E) out.write(0X2C);
else out.write(dato);
}
}
if(dati == "link"){
int virgola = 0;
dato = in.read();
while(dato != 0X65){
if(virgola < 4){
if(dato == 0X2C) virgola++;
if(dato == 0X09 || dato == 0X0D){}
else out.write(dato);
}
else{
while(dato != 0X0D) {
dato = in.read();
}
virgola = 0;
out.write(0X0D);//nl
out.write(0X34);//4
out.write(0X20);//space
}
dato = in.read();
}//while link
end = true;
}//if link
}//while file
//testing the write() method
for(int i=0; i<1000; i++){
out.write(0X2C); //tricky part! this output will be always written!! WTF!
}
in.close();
out.close();
JOptionPane.showMessageDialog(null, "Done!", "Done!" , JOptionPane.INFORMATION_MESSAGE);
}
catch (IOException e){
System.out.println(e + "\n");
}
}
}
在寫上面的代碼
(INT:
public static void main (String[] args){
File output = new File("MyOutputDirectoryHere");
try{
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream (output)));
for(int i=0; i<1000; i++){
dos.write(i);
}
dos.close();
}
catch (IOException e){
System.err.println(e);
}
}
和它完美的作品
但這個爛攤子裏b)方法不會在桌面上的輸出文件中寫入任何內容,除了最後編碼的for循環...代碼的那部分工作正常...
我不知道該怎麼做。請幫忙。
您是否確認'dati'確實等於「vnod」或「link」? – vegasje
你有一個示例IPL文件嗎? –
查看我的答案以解決您的問題,這是一個非常典型的陷阱,例如如果有人從C++到Java。此外,我建議你熟悉使用調試器。 – Joe