?
Solved

Read .txt file to a command line

Posted on 2005-03-03
9
Medium Priority
?
181 Views
Last Modified: 2010-03-31
Hi, I want to read the contents of a .txt file and print them to the command line. How do I do it in Java?
0
Comment
Question by:MPerre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 16

Expert Comment

by:Joe
ID: 13456702
String line = null;
FileReader fr = null;
BufferedReader br = null;

fr = new FileReader("c:/mytxt.txt");
br = new BufferedReader(fr);

while ((line = br.readLine()) != null){
               System.out.println(line);

}

fr.close();
br.close();


Joe
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 2000 total points
ID: 13456859
0
 

Author Comment

by:MPerre
ID: 13459038
Dosent work Joe. I need the complete code not just snipits.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 30

Expert Comment

by:Mayank S
ID: 13459171
It is against EE rules to ask for full code just like that. It is treated as homework no matter whether it is or not (read the member agreement). Come up with some coding efforts of your own and then we can help you more.
0
 
LVL 1

Expert Comment

by:sanjusk
ID: 13460688
Although i pasting the class file that does what you want. i don't know the urgency. But i agres what 'mayankeagle' says.

import java.io.*;

public class test{
  // Throw exceptions to console:
  public static void main(String[] args)
  throws IOException {
       BufferedReader in =
      new BufferedReader(
        new FileReader("IOStreamDemo.java"));
    String s, s2 = new String();
    while((s = in.readLine())!= null)
      s2 += s + "\n"; // System.out.println(s) -- whatever ever you like
    in.close();
    System.out.println(s2);
}
}
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month10 days, 19 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question