简体   繁体   English

DataOutputStream.write(int b)不写

[英]DataOutputStream.write(int b) doesn't write

i'm writing a java program in eclipse that take a file as input and write a new file as output follow some logic. 我在eclipse中编写一个Java程序,该程序将文件作为输入,并将新文件写入作为输出,遵循一些逻辑。

i use datainputstream for the input file and dataoutputstream for the output file. 我将datainputstream用于输入文件,并将dataoutputstream用于输出文件。 i use those for the readln() method that works well. 我将它们用于效果很好的readln()方法。

now i have a problem with the write() method, simply it doesn't write anything! 现在我对write()方法有问题,只是它什么也没写! i've tryed also randomAccess for the input, and/or bufferedoutputstream for the output. 我也尝试了randomAccess作为输入,和/或bufferedoutputstream作为输出。

i've tryed in a separate java project this: 我已经在一个单独的Java项目中尝试过此操作:

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);
    }   
}

and it works perfectly 而且效果很好

but inside this mess: 但在这个烂摊子里面:

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");
    }
}
}

in the code above the write(int b) method doesn't write anything in my output file on desktop, except for the for loop coded at the end... that part of code works well... 在上方的代码中,write(int b)方法不会在桌面上的输出文件中写入任何内容,除了末尾编码的for循环外……那部分代码效果很好...

i dont know what to do. 我不知道该怎么办。 please help. 请帮忙。

In java, you cannot compare strings using == . 在Java中,不能使用==比较字符串。 You have to use equals() 您必须使用equals()

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

The == just compare the reference (like a pointer) of equalness and not it's referenced content. ==只是比较相等的引用(如指针),而不是引用的内容。

See also here . 另请参阅此处

You should close all your streams in finally blocks. 您应该在finally块中关闭所有流。 Clearly the output stream isn't being closed here. 显然,这里没有关闭输出流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM