java - DataOutputStream.write(int b) 不写入

标签 java file-io dataoutputstream

我正在 eclipse 中编写一个 java 程序,它接受一个文件作为输入,并按照一定的逻辑编写一个新文件作为输出。

我使用 datainputstream 作为输入文件,使用 dataoutputstream 作为输出文件。 我将它们用于 readln() 方法,效果很好。

现在我的 write() 方法有问题,它没有写任何东西! 我还尝试过使用 randomAccess 作为输入,和/或使用 bufferedoutputstream 作为输出。

我在一个单独的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);
    }   
}

而且效果很好

但是在这个困惑之中:

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

在上面的代码中,write(int b) 方法不会在桌面上的输出文件中写入任何内容,除了最后编码的 for 循环...这部分代码运行良好...

我不知道该怎么办。请帮忙。

最佳答案

在java中,不能使用==来比较字符串。您必须使用equals()

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

== 只是比较引用(如指针)的相等性,而不是比较其引用的内容。

另请参阅here .

关于java - DataOutputStream.write(int b) 不写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21034059/

相关文章:

Java 导出服务器 : (SVGConverter. java:convert:108) 响应:ReferenceError:找不到变量:Highcharts

java - java中自定义异常中的父类(super class)构造函数

java - 通过 JAVA DataOutputStream 发送 key

Java DataOutputStream/DataInputStream OutOfMemoryError

java - 有什么方法可以从层次结构中更高的类中退出命名循环(或其他方式)?

java - 在java中哪里使用 "this"?

java - 使用ObjectOutputStream删除特定数据

java - 在控制台上打印并打印到文件中

c++ - QTextStream格式问题

java - 无法写入超出特定大小的 DataOutputStream - OutOfMemoryError