Home > Blockchain >  Accessing texts files in Java
Accessing texts files in Java

Time:10-14

I am trying to access text files in Java, however, I don't know how to do to that and apply it to a 2D array. I have done it before for a String in a pig latin coding assignminet e.x :

    public static void piglatenizeFile(Scanner infile, String filename) throws IOException{
       PrintWriter outFile = new PrintWriter(filename);
       while(infile.hasNext()){
          String s = infile.nextLine();
          String p = pig(s);
          outFile.println(p);
       }
       outFile.close();
    }

However this is what I have to do:

    /* creates scanner for filename
       creates 2D array based on row/col
       and characters in file
   */
   public static char[][] read(String filename) throws FileNotFoundException{  
   }

How does it work with a 2D array?

CodePudding user response:

Java doesn't have 2D arrays. It merely has arrays. Of course, if you have an array whose component type is also an array, it acts a lot like a 2D array, even though it isn't. For example, in java there is no type that describes an array-of-arrays such that it's 'rectangular'. You can have:

char[][] x = ...;
x[0].length == 5;
x[1].length == 7;

Which isn't a 2D array. char[][] is just a 1D array, whose components are char arrays.

There is some very basic syntax sugar available: new char[10][20] is shorthand for: "Make a new array of char arrays with room for 10 arrays. Then, for each slot, execute new char[20] and assign the result to that slot".

But don't let that confuse you. It's just shorthand, is all. It's just:

char[][] x = new char[20][10];

and

char[][] x = new char[20][];
for (int i = 0; i < x.length; i  ) {
  x[i] = new char[10];
}

It's identical (even at the bytecode level, in fact). 2D arrays do not exist and thus you don't need to waste brainspace and learn about what they are. You just need to know about the above syntax sugar and always reason about the code knowing that 2D arrays aren't really a thing, they're just arrays-of-arrays.

Keep that in mind and you can answer this question on your own, using the skills you already have.

That, and know that "foo".toCharArray() turns a string into a char array. There is no difference between a char array and a string as far as array assignments are concerned:

  • String[] is a 1D array which only allows you to assign expressions of type String in them.
  • char[][] is a 1D array which only allows you to assign expressions of type char[] in them.
  • String is just an object type. So is char[]. arrays are objects.
  • someStr.toCharArray() is an expression with type char[].

CodePudding user response:

The text files are usually read by lines into a list/array of String and each line can be converted to char[] using String::toCharArray, so a jagged array of char[] can be returned.

Java also provides convenient NIO API to read files as Stream<String> which can be converted in the desired char[][] result:

public static char[][] readFile(String filename) throws IOException {
    return Files.lines(Paths.get(filename)) // Stream<String>
            .map(String::toCharArray)       // Stream<char[]>
            .toArray(char[][]::new);        // char[][] created
}
  • Related