1

I have a input file that contains on the first line an integer (n) and on the second line n integers.

Example:

7
5 -6 3 4 -2 3 -3

The problem is that my data gets "corrupted". I've been using new File(path) but I'm trying to submit my code to an online compiler to run some tests on it and new File(path) represents a security problem there. Thank you.

public static void main(String[] args) throws IOException {
        FileInputStream fin=new FileInputStream("ssm.in");
        int n;
        n = fin.read();
        a = new int[100];
        for(int i=1;i<=n;i++)
            a[i]=fin.read();
        fin.close();
}

Edit: When I try to print the array a, the result should be:

5 -6 3 4 -2 3 -3

Instead, it is:

13 10 53 32 45 54 32 51 32 52 32 45 50 32 51 32 45 51 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
5
  • What's the question again? Commented Dec 8, 2014 at 11:14
  • Could you explicitly tell us your desired result VS what you actually got? In what sense was your data corrupt? The value didn't turn out the same or did it not work at all? Commented Dec 8, 2014 at 11:15
  • fin.read() returns you ascii of character and not 7 as integer Commented Dec 8, 2014 at 11:22
  • I think the security problem is related to the online compiler (don't see why you use that) and accessing a file. Are you then also executing the code online? Then new File would access the file system on the server and not your local machine... Commented Dec 8, 2014 at 11:22
  • The doc for FileInputStream#read() says the following: Reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available. And also returns: the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached. Commented Dec 8, 2014 at 11:27

1 Answer 1

1

Probably your file contains the data as plain text (presumably ASCII), like this:

7
5 -6 3 4 -2 3 -3

If you open the file with FileInputStream and use the read() method to read a single byte from the file, what you actually get is the ASCII character's number. The many -1 which you see are meaning that there's nothing left from the file to be read.

What you actually want to do is convert the ASCII text to a number. For this, you should not read binary data but something that involves char or String, like FileReader and BufferedReader. And you need to involve Integer.parseInt().

The following listing shows how to read a single number from a text file:

import java.io.*;

public class ReadNumber {
    public static void main(final String... args) throws IOException {
        try (final BufferedReader in = new BufferedReader(new FileReader(args[0]));
            final String line = in.readLine();
            final int number = Integer.parseInt(line);
            System.out.format("Number was: %d%n", number);
        }
    }
}

You can change this source code to your needs accordingly. You might also want to read about the Scanner class and the String.split() method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.