How to read 5 lines from a text file


Confused 678

How would I use a buffered reader or scanner to read the first 5 lines from a text file in java? this is my code

        final int assumedLineLength = 16;


    File file = new File("src/hw7p1/Acronyms.txt");
     HashMap<String, String> hashMap = new HashMap<String, String>((int)(file.length() / assumedLineLength) * 2);
    BufferedReader reader = null;
    int linecount = 0 ;
    String eachLine = null;
    try {
    reader = new BufferedReader(new FileReader(file));

    for ( eachLine = reader.readLine(); 
            eachLine != null; 
            eachLine = reader.readLine()) {
        hashMap.put(eachLine, " ");
        linecount++; int i = 0;

    }


    TreeMap<String, String> sorted = new TreeMap<>(hashMap);

     Set<Entry<String, String>> sortings = sorted.entrySet();

    for(Entry<String, String> sort : sortings){ 
        System.out.println(sort.getKey() + " " + sort.getValue());

    }

     }catch (IOException e) {
         e.printStackTrace();
     }

It prints out the text file, but all I want to do is have it print the first 5 lines. Any help would be greatly appreciated.

Ryan_L

In your for loop, if you only need 5 lines, don't use eachLine. use

for(int i=0; i<5; i++)
{
  //read the file
}

Related


Read lines of text from a file

frankly speaking: I have a text file containing 5 customers (1 per line), customer 1, customer 2, customer 3, customer 4 and customer 5. Using the following code, it reads 5 lines of text perfectly; import java.io.*; public class CustomerIO { public void m

python: read lines from compressed text file

delete_this_account: Is it easy to use python to read a line from a gz compressed text file without extracting the file completely? I have a text.gz file about 200mb. When I extract it, it becomes 7.4gb. This is not the only document I have to read. For the wh

Read random lines from text file in go

but: I am using to encoding/csvread and parse very large .csv files. I need to randomly select lines and pass some tests. My current solution is to read the whole file reader := csv.NewReader(file) lines, err := reader.ReadAll() Then select the row at random

How to read different lines from a text file at the same time

Su Linda I need to read the file line by line so that it reads the first line, processes it, then accepts the second line, processes it, and so on. I know how to read a text file line by line: for(line <- Source.fromFile("file.txt").getLines()) { insert(lin

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

python: read lines from compressed text file

delete_this_account: Is it easy to use python to read a line from a gz compressed text file without extracting the file completely? I have a text.gz file about 200mb. When I extract it, it becomes 7.4gb. This is not the only document I have to read. For the wh

Read lines from text file into array

jms1980 I have a text file with the following lines: (0,0) -180.000 77.500 -999.000 -999.000 -999.000 2740.831 45.000 -0.001 -0.001 0.000 458.138 45.000 -999.000 (1,0) -179.500 77.500 -999.000 -999.000 -999.000 2740.831 45.000 -0.001 -0.001

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

How to read different lines from a text file at the same time

Su Linda I need to read the file line by line so that it reads the first line, processes it, then accepts the second line, processes it, and so on. I know how to read a text file line by line: for(line <- Source.fromFile("file.txt").getLines()) { insert(lin

Read lines of text from a file

frankly speaking: I have a text file containing 5 customers (1 per line), customer 1, customer 2, customer 3, customer 4 and customer 5. Using the following code, it reads 5 lines of text perfectly; import java.io.*; public class CustomerIO { public void m

batch file to read lines from text file

microphone I am new to batch scripting and I would like your help with the following: I have a text file that looks like this: E8-40-F2-3C-2E-FD,w7-301 E8-40-F2-AC-D4-E0,w7-302 E8-40-F2-AB-D9-F6,w7-303 E8-40-F2-3C-2F-2D,w7-304 E8-40-F2-AB-D0-D7,w7-305 My goal

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

How to read text from a file but ignore certain lines in C#?

animal 91 I'm just learning C# and I need to read text from a file into an array, but ignore lines starting with some special characters. I am using ReadAllLines to read text. What am I doing: string file = openFileDialog1.FileName; file_txtbx.Text = file; try

batch file to read lines from text file

microphone I am new to batch scripting and I would like your help with the following: I have a text file that looks like this: E8-40-F2-3C-2E-FD,w7-301 E8-40-F2-AC-D4-E0,w7-302 E8-40-F2-AB-D9-F6,w7-303 E8-40-F2-3C-2F-2D,w7-304 E8-40-F2-AB-D0-D7,w7-305 My goal