2014-01-10 3 views
0

저는 이클립스에서 파일을 입력으로 사용하고 출력으로 새 파일을 작성하는 Java 프로그램을 작성하고 있습니다. 일부 논리를 따릅니다.DataOutputStream.write (int b)는 쓰지 않습니다.

나는 입력 파일에 대해 datainputstream을 사용하고 출력 파일에 대해서는 dataoutputstream을 사용합니다. 잘 작동하는 readln() 메서드에 사용합니다.

이제 write() 메서드에 문제가 생겼습니다. 아무 것도 쓰지 않습니다. 입력에 대해 randomAccess를 시도한 다음 출력에 대해 bufferedoutputstream을 tryed했습니다.

내가 별도의 자바 프로젝트에 tryed 한이 : 쓰기 위의 코드에서

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 루프를 제외한 나머지 코드를 데스크톱의 출력 파일에 쓰지 않습니다 ... 코드 부분이 잘 작동합니다 ...

나는 무엇을 해야할지 잘 모릅니다. 도와주세요.

+0

'dati'가 실제로 "vnod"또는 "link"와 같은 것을 확인 했습니까? – vegasje

+0

예제 IPL 파일이 있습니까? –

+0

내 답변을 참조하여 문제를 해결하십시오. 매우 일반적인 함정입니다. 누군가 C++에서 java에 이르면. 또한 디버거를 사용하여 익숙해지기를 권장합니다. – Joe

답변

1

자바에서는 ==을 사용하여 문자열을 비교할 수 없습니다. 당신은 equalness의 (포인터와 같은)에 대한 참조를 비교 equals()

if (dati.equals("vnod")) ... 

==를 사용해야하며 내용을 참조 아니에요.

도 참조하십시오. here

+0

사실, 나는 그가 특히 가지고있는 문제와 관련이 있는지 보지 못합니다. –

+0

@MartinTuskevicius 모든 write() 문은 == 연산자로 문자열을 비교하는 if 문 안에 있기 때문에 중요합니다. –

+1

그의 코드를 보면 write() 호출은 모두 그런 종류의 if() 조건 이후입니다. if() 컨텍스트가 결코 true가되지 않는 한, 코드는 절대로 도달하지 않습니다. – Joe

0

모든 스트림을 finally 블록으로 닫아야합니다. 분명히 출력 스트림이 여기서 닫히지 않습니다.