V
V
Vlad1612014-02-23 01:43:18
Java
Vlad161, 2014-02-23 01:43:18

Why does the program write hieroglyphs to a file instead of numbers?

Having nothing to do, I decided to make a program that takes numbers from a file and rounds them to integers. For example 3.151361 = 3, 13.836724 = 14 etc. But instead of integers, he writes hieroglyphs, like this:
b169f508fd.jpg
What's the problem? First time I'm facing this.

import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;

public class Main {

    BufferedReader reader = null;
    FileInputStream inputStream = null;
    FileOutputStream outputStream = null;
    ArrayList<Double> doubleNumbers = new ArrayList<Double>();
    ArrayList<BigInteger> intNumbers = new ArrayList<BigInteger>();

    int count = 0;

    public static void main(String[] args) throws IOException {
        Main main = new Main();
        main.start();
    }

    public void start() throws IOException {

        readFile();
        okryglenieChisel();
        writeFile();
    }

    public String readFilesName() throws IOException {
        reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Введите название файла " + ++count);
        String fileName = reader.readLine();
        return fileName;
    }

    public void readFile() throws IOException {
        inputStream = new FileInputStream(readFilesName());
        String s = "";
        while (inputStream.available() > 0) {
            char d = (char) inputStream.read();

            if (d != ' ') {  //Да, да, это не лучший метод, лучше разбить их методом split, это сделаю позже
                s += d;
            } else {
                doubleNumbers.add(Double.parseDouble(s));
                s = "";
            }
        }
    }

    public void writeFile() throws IOException{
        outputStream = new FileOutputStream(readFilesName());
        for (BigInteger i : intNumbers) {
            Integer integ = new Integer(i.intValue());
            outputStream.write(integ);
        }

    }

    public void okryglenieChisel() {
        for (Double x : doubleNumbers) {
            BigDecimal dec = new BigDecimal(x);
            intNumbers.add(dec.toBigInteger());
        }
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FoxInSox, 2014-02-23
@Vlad161

The FileOutputStream class writes "bare bytes" to the file. Moreover, your ints are truncated to a byte.

@Override
public void write(int oneByte) throws IOException {
     write(new byte[] { (byte) oneByte }, 0, 1);
}

Text/characters in computer representation and numbers are different things. Those. for example the text "256" is not the number 256, the text "256" is three characters, and each character is 8 bytes in UTF-8 encoding.
As a result, the text editor simply displays your truncated bytes, which refer to different characters ("hieroglyphs").
You either need to cast your ints to a string, and then take an array of bytes:
Or wrap the FileOutputStream in a PrintStream and write the data to the file using the print method. Which calls the same getBytes:
public synchronized void print(String str) {
    if (out == null) {
        setError();
        return;
    }
    if (str == null) {
        print("null");
        return;
    }

    try {
        if (encoding == null) {
            write(str.getBytes());
        } else {
            write(str.getBytes(encoding));
        }
    } catch (IOException e) {
        setError();
    }
}

ps close streams, and don't use wrappers (Integer) if you don't need to.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question