-4

How do I create an array whose size is equal to the size of a file? The file name is passed as argument to main.

public static void main(String[] args){
    File text = new File(args[0]); //file input
    Scanner input = null;
    try {
        input = new Scanner(text);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    int[] A = new int[??????]; //what should be size 2 match file    
}
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
ajsum0
  • 5
  • 2
  • possible duplicate of [Get size of folder or file](http://stackoverflow.com/questions/2149785/get-size-of-folder-or-file) – weston Feb 12 '15 at 13:09
  • 2
    Why an `int` array? What do you want to do with it? – fge Feb 12 '15 at 13:09
  • 4
    When you were typing your question, there was a big orange **How to Format** box to the right with a list of formatting tips, including how to format code. Worth a read. Also there was a full toolbar of formatting tools. And a **[?]** button to provide help. And a preview area underneath the text box where you could review your question before posting it. Please make use of these things, they're there for a reason. – T.J. Crowder Feb 12 '15 at 13:10
  • @fge
    while(input.hasNextInt()){
    for(int i = 0;i A[i]= input.nextInt();
    if(A[i]%2 == 0)System.out.println("1");
    else System.out.println("0");
    }
    i want to use it in this code, but when i run this code with any random size of Array A, and if the number of entries in the file is less than the size of A then it throws "No such elements exception" so i figured if i can get the size of file to be the size of A it wont happen, right?
    – ajsum0 Feb 12 '15 at 13:13
  • 1
    Don't post large code in comment. It is badly formatted and most probably should be part of question itself. Use [edit] option below your question to put it in your question. Also don't forget to use `{}` button from editor toolbar to format your code. – Pshemo Feb 12 '15 at 13:15
  • This is most likely an [XY Problem](http://meta.stackexchange.com/q/66377). Why do you need an array of ints to be the same size of a file? – RealSkeptic Feb 12 '15 at 13:17
  • (based on your comment) If you don't know how many elements you will have to store inside array then don't use List instead. Also `for (int i = 0; i < A.length; i++) { A[i] = input.nextInt();` will try to read `A.length` integers from `input` but if there will be no `A.length` elements you will get `NoSuchElementException`. Use `while(input.hasNextInt) {list.add(input.nextInt()); }` instead. – Pshemo Feb 12 '15 at 13:33

2 Answers2

2

Use java.nio.file:

final Path path = Paths.get(args[0]);
// use Files.size(path)

Note that you can read directly the contents of a regular file into a byte array:

final byte[] content = Files.readAllBytes(path);
fge
  • 119,121
  • 33
  • 254
  • 329
1

Try length() to fetch the length of your file..

text.length()

in your case..

Saurabh Rajpal
  • 1,537
  • 9
  • 14
  • 1
    it will return length in bytes, but size of integer is 4 times larger than byte, so created array will not be same size as file. – Pshemo Feb 12 '15 at 13:17