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
docforFileInputStream#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.