Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.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
Java 为什么writer不将数据写入文件?_Java - Fatal编程技术网

Java 为什么writer不将数据写入文件?

Java 为什么writer不将数据写入文件?,java,Java,当我运行它时,它会转到名为niceJob.txt的文件,数据来自的文件名为file2.txt,其中包括 niceJob.txt 40 20 1 1 5 7 45 1 2 3 4 5 6 7 8 9 77 88 99 23 34 56 当我打开niceJob.txt时,它会显示 X8 我真的很困惑为什么会发生这种事,以及是如何发生的。代码如下: import java.io.*; import java.util.Scanner; public class JH1_00668860 {

当我运行它时,它会转到名为niceJob.txt的文件,数据来自的文件名为file2.txt,其中包括

niceJob.txt 40
20 1 1 5 7 45 1 2 3 4 5 6 7 8 9 77 88 99 23 34 56
当我打开niceJob.txt时,它会显示

X8

我真的很困惑为什么会发生这种事,以及是如何发生的。代码如下:

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 print

    public static void process(String inputFilename) {
        String fileoutputname = null;
        FileInputStream file = null;
        Scanner scan = null;
        FileOutputStream outputFilename = null;
        FileWriter 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 FileWriter(fileoutputname);
            while (scan.hasNextInt()) {
                if (scan.nextInt() >= 0) {
                    // System.out.println(scan.nextInt() + "asfs");
                    ps.write(scan.nextInt());
                    ps.flush();

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

    }
}
  • 您在
    的迭代中多次(2次)调用
    scan.nextInt()
    ,而
    循环并使用
    scan.nextInt()
    的第二次调用写入文件
    ps.write(scan.nextInt())
    ;它跳过每个可选的
    整数
  • 您正在将一个
    整数
    传递给
    ps。write()
    ,而是传递一个
    字符串

    import java.util.Scanner;
    import java.io.*;
    
    
    public class Main {
        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 print
    
        public static void process(String inputFilename) {
            String fileoutputname = null;
            FileInputStream file = null;
            Scanner scan = null;
            FileOutputStream outputFilename = null;
            FileWriter 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 FileWriter(fileoutputname);
                int currentInt = -1;
                while (scan.hasNextInt()) {
                    currentInt = scan.nextInt();
                    if (currentInt >= 0) {
                        //System.out.println(currentInt + "asfs");
                        ps.write(String.valueOf(currentInt));
                        ps.flush();
    
                    } 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("sample.txt");
    
        }
    }
    
  • 输出:

    niceJob.txt      asfasdfasdfasdf
    A file was created
    4020115745123456789778899233456
    

    currentInt=-1有什么用?如果您想多次使用
    scan.nextInt()
    ,就像在
    if条件中一样,在屏幕上打印它,将它写入文件等,而不需要多次调用
    scan.nextInt()
    (如果多次调用它,它会一直移动到最后一次
    int
    。例如:最初
    scan.nextInt()
    5237
    的情况下返回5,如果第二次调用
    scan.nextInt()
    ,它会返回2,第三次调用返回3,第四次调用返回7,最后一个元素),您可以存储
    scan.nextInt()的返回值<代码> >代码> > Currimint INT/COM>并多次引用,不必担心上述情况。- 1是将该变量初始化为某个值。考虑一个不初始化IE <代码> int Currutint;< /C> >的情况,在下一行中,如果您想打印“代码> Currimint INT/CODE >,您将得到一个错误。aying
    变量可能尚未初始化
    。希望这对您有所帮助。请了解如何使用资源进行尝试:这将使您的代码不那么冗长。