簡體   English   中英

為什么編寫器不將數據寫入文件?

[英]Why is writer not writing the data into the file?

當我運行時,它將轉到名為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");

    }
}
  1. 您在while循環迭代中多次調用scan.nextInt() (2次),並使用第二次調用scan.nextInt()寫入文件ps.write(scan.nextInt()) 它跳過每個備用integer
  2. 您將一個integer傳遞給ps.write() ,而是傳遞一個string

     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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM