简体   繁体   中英

FileWriter doesn't write integers into file

I am trying to read 2 input files containing integers(even duplicates are considered) and trying to find common integers and write them to the output file.

input1.txt

01
21
14
27
31
20
31

input2.txt

14
21
27
08
09
14

Following is the code I tried:

public static void main(String[] args) throws NumberFormatException {
        try {
            BufferedReader inputFile1 = new BufferedReader(new FileReader(new File("src/input1.txt")));
            BufferedReader inputFile2 = new BufferedReader(new FileReader(new File("src/input2.txt")));
            FileWriter fileCommon = new FileWriter("src/common.txt");
            String lineInput1;
            String lineInput2;
            int inputArray1[] = new int[10];
            int inputArray2[] = new int[10];
            int index = 0;
            while ((lineInput1 = inputFile1.readLine()) != null) {
                inputArray1[index] = Integer.parseInt(lineInput1);
                index++;
            }
            index = 0;
            while((lineInput2 = inputFile2.readLine()) != null) {
                inputArray2[index] = Integer.parseInt(lineInput2);
                index++;
            }

            for (int a = 0; a < inputArray1.length; a++) {
                for (int b = 0;b < inputArray2.length; b++) {
                    if(inputArray1[a] == inputArray2[b]) {
                        fileCommon.write(inputArray1[a]);
                    }
                }
            }
            inputFile1.close();
            inputFile2.close();
            fileCommon.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

I don't understand where I am making mistake. I am not getting any errors and the output file that is generated is empty.

output expected are common integers in both files

14
21
27

Remember, that FileWriter 's write(int c) accepts an integer representing a character code from either a specified charset or the platform's default charset , which is mostly extensions of ASCII (for example, in Windows, default charset is Windows-1252 which is an extension of ASCII).

which means, that you actually don't have any (semantical or syntactical) problem per se, and you're writing into file successfully , but . you're writing some special characters which you can't see afterwards.

If you'll invoke write(..) with some integer representing Latin character (or symbol) in the ASCII table, you'll see that it'll write actual English letter (or symbol) into your file.

For instance:

fileCommon.write(37); //will write `%` into your file.
fileCommon.write(66); //will write `B` into your file.

In your code, you're only writing 21 , 14 and 27 into your file, and as you can see from the ASCII table:

  • Decimal 21 represents Negative Acknowledgment
  • Decimal 14 represents Shift-out
  • Decimal 27 represents Escape

FileWriter.write(int) will write a single character, in your case 14, 21, and 27 are all control characters that would not be visible in a text file.

common.write("" + arr1[a]);

Should write the string representation. You'll find some other problems though, such as missing line endings and repeated values, but this should get you started.

Here's the thing.

  1. The write(int c) method of FileWriter is not actually write an int value, but write an ASCII code of a single character.For example, write(53) will write a "5" to a file.

    In your code, you are acctually writting some symbols.You can use write(String str) method of FileWriter or just use BufferedWriter class to achieve you goal.

  2. The result of the write value is acctually "21141427" by your code, so you have to remove the repeat value when write it and line feed after write each value.

Sorry for the poor English.

You can read String s from the original input files, instead of int s, and use the String.equals(Object):boolean function to compare Strings.

Then you won't need to parse from String to int, and convert an int to string back when writing to the file.

Also note that writing an int will write the unicode char value to the file, not the number as a string.

The problem is the common.write line. It should be as follows.

                   common.write(String.valueOf(arr1[a])+"\n");

Additionally, This would perform much better if you put all of the data from the first file into a Map vs an array then when reading the second file just check the map for the key and if it exists write to common.

If you are dead set on using an array you can sort the first array and use a binary search. This would also perform much better than looping through everything over and over.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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