JDB
JDB

Reputation: 65

Reading a file in Java

I have the following code to open and read a file. I'm having trouble figuring out how I can have it go through and print the total number of each character in the file, print the first and last character, and print the character exactly in the middle of the file. What's the most efficient way to do this?

This is the main class:

import java.io.IOException;

public class fileData {

public static void main(String[ ] args) throws IOException {
    String file_name = "/Users/JDB/NetBeansProjects/Program/src/1200.dna";

    try {
        ReadFile file = new ReadFile(file_name);
        String[] arrayLines = file.OpenFile();

        int i;
        for (i=0; i<arrayLines.length; i++)
        {
            System.out.println(arrayLines[i]);
        }
    }

    catch (IOException e) {
        System.out.println(e.getMessage()) ;
    }

}


}

and the other class:

import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;


public class ReadFile {

    private String path;

public ReadFile (String file_path)
    {
     path = file_path;
    }

public String[] OpenFile() throws IOException
    {
        FileReader fr = new FileReader(path);
        BufferedReader textReader = new BufferedReader(fr);

        int numberOfLines = readLines();
        String[] textData = new String[numberOfLines];

        int i;

        for(i=0; i<numberOfLines; i++)
        {
            textData[i] = textReader.readLine();
        }

        textReader.close();
        return textData;
    }

    int readLines() throws IOException
    {
        FileReader file_to_read = new FileReader(path);
        BufferedReader bf = new BufferedReader(file_to_read);

        String aLine;
        int numberOfLines = 0;

        while (( aLine = bf.readLine() ) != null)
        {
            numberOfLines++;
        }

        bf.close();
        return numberOfLines;
    }

Upvotes: 0

Views: 315

Answers (3)

filip-fku
filip-fku

Reputation: 3265

The easiest way to understand I can think of is to read the entire file in as a String. Then use the methods on the String class to get the first, last, and middle character (character at index str.length()/2).

Since you are already reading in the file a line at a time, you can use a StringBuilder to construct a string out of those lines. Using the resulting String, the charAt() and substring() methods you should be able to get out everything you want.

Upvotes: 1

Bohemian
Bohemian

Reputation: 424953

These few lines of code will do it (using Apache's FileUtils library):

import org.apache.commons.io.FileUtils;

public static void main(String[] args) throws IOException {
    String str = FileUtils.readFileToString(new File("myfile.txt"));
    System.out.println("First: " + str.charAt(0));
    System.out.println("Last: " + str.charAt(str.length() - 1));
    System.out.println("Middle: " + str.charAt(str.length() / 2));
}

Anyone who says "you can't use libraries for homework" isn't being fair - in the real world we always use libraries in preference to reinventing the wheel.

Upvotes: 1

Steven Mackenzie
Steven Mackenzie

Reputation: 386

Some hints which might help.

  1. A Map can be used to store information about each character in the alphabet.
  2. The middle of the file can be found from the size of the file.

Upvotes: 1

Related Questions