Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么.write不将int添加到文件中?JAVA_Java_Io - Fatal编程技术网

为什么.write不将int添加到文件中?JAVA

为什么.write不将int添加到文件中?JAVA,java,io,Java,Io,我有一个关于File.io库的问题。所以在一节课上我有一个作业 我在写作业中需要在输出文件中添加int的部分时遇到了麻烦。这是我的密码 import java.io.*; import java.util.Scanner; public class JH1_00668860 { public static void printToScreen(String filename) { Scanner scan = null; try {

我有一个关于File.io库的问题。所以在一节课上我有一个作业

我在写作业中需要在输出文件中添加int的部分时遇到了麻烦。这是我的密码

import java.io.*;
import java.util.Scanner;

public class JH1_00668860 {
    public static void printToScreen(String filename) {
        Scanner scan = null;
        try {
            FileInputStream fis = new FileInputStream(filename);
            scan = new Scanner(fis);
            while (scan.hasNextLine()) {
                System.out.println(scan.nextLine());
            }
        } catch (FileNotFoundException e) {
            System.out.println("printToScreen: can't open: " + filename);
        } finally {
            if (scan != null)
                scan.close();
        }
    }// end of prin

    public static void process(String inputFilename) {
        String fileoutputname = null;
        FileInputStream file = null;
        Scanner scan = null;
        FileOutputStream outputFilename = null;
        OutputStream ps = null;
        try {
            file = new FileInputStream(inputFilename);
            scan = new Scanner(file);
            fileoutputname = scan.next();
            System.out.println(fileoutputname + "asfasdfasdfasdf");





            outputFilename = new FileOutputStream(fileoutputname);
            ps = new FileOutputStream(fileoutputname);
            if (scan.hasNextInt() && scan.nextInt() >= 0) {
                System.out.println(scan.nextInt() + "asfs");
                ps.write(scan.nextInt());





            } else {
                System.out.println("You have ran out of data or you have a bad value");
            }

            System.out.println("A file was created");
        } catch (FileNotFoundException e) {
            System.out.println("You ran into an exception :" + e);
        } catch(IOException e) {
            System.out.println("You ran into an exception :" + e);

        } finally {
            try {
                if (file != null) {
                    file.close();

                }
                if (outputFilename != null) {
                    outputFilename.close();

                }
                if (ps != null) {
                    ps.close();

                }
//              FileInputStream st = new FileInputStream(fileoutputname);
//              int contents = st.read();
//              while (scan.hasNextInt()) {
//                  System.out.print(contents);
//              }
                if (scan != null) {
                    scan.close();
                }
                printToScreen(fileoutputname);

            } catch (IOException e) {
                System.out.println("there was an exception");
            }

        }

    }

    public static void main(String args[]) {
        process("file2.txt");

    }
}
当我运行它时,控制台显示 然后我转到我的计算机上名为niceJob.txt的文件,它以空文件开始,Eclipse说它将被更改,但当我“重新加载”时,什么也没有显示


有人能帮我调试这个bug吗(或者如果它是发生在其他事情上的话?)。在您的代码
ps.write(scan.nextInt())之后感谢您
尝试放置
ps.write.flush()
ps.flush()

编辑: 如果仍不起作用,请添加导入:

import java.io.BufferedWriter;
import java.io.FileWriter;
并更改
ps.write(scan.nextInt())

BufferedWriter writer = new BufferedWriter(new FileWriter(inputFilename)); 
writer.write(Integer.toString(scan.nextInt));  
writer.newLine();
writer.flush();

我马上看到的一个问题是,你调用了三次
.nextInt()
,只向文件写入了一次,所以你扔掉了其中两次读取。“有人能帮我调试这个bug吗?”-您是否尝试过使用IDE的调试器?我尝试过刷新和写入,但数据不会进入文件,我的意思是它们都应该是ps.write(scan.nextInt());+ps.write.flush();在下一行或ps.write(scan.nextInt());+ps.flush();