0

I have a text file in my desktop named "MyText.txt" and it have some contents in it.

Since i am learning JAVA i thought of simply creating an console application which will read the contents from the txt file and then show it in console.

But since i am new to this ,so i am unable to figure out the way it works.

Can anyone get me into a right direction.

All help will be appreciated.

Vipin Nair
  • 517
  • 4
  • 9
  • 33

3 Answers3

1

There are many alternatives for doing this. A higher level thing could be using a Scanner class. I assume, since you are learning java, you might have come across Scanner class for reading the input from console. You can use the Scanner same way to read the file also.

You can use Scanner#nextInt(), Scanner#next(), etc... methods for reading the input. You can use args[] array for taking the command line arguments.

Since, you haven't mention exactly what kind of way your data is stored in file, it's hard to give a working example.

import java.util.Scanner;

public class Tester { 
  public static void main(String args[]) {
    if (args.length > 0) {
      Scanner sc = new Scanner(new File(args[0]));
      //use here the functions such as sc.nextInt() and so on
    }
  }
}

Link to the docs: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

If you are reading big file in java (see my answer to other question): file size too big for java:

Working Example:

This program prints the file contents to console.

Execute this program using java Tester yourFileName after compilation.

import java.util.*;
import java.io.*;

public class Tester { 
  public static void main(String args[]) {
    try {
      if (args.length > 0) {
        Scanner sc = new Scanner(new File(args[0]));
        while (sc.hasNext()) {
          System.out.println(sc.next());
        }
      } else {
        System.out.println("No file name given");
      }
    }
    catch(FileNotFoundException e) {
      e.printStackTrace();
    }
  }
}
Tomasz Szymulewski
  • 2,003
  • 23
  • 23
pinkpanther
  • 4,770
  • 2
  • 38
  • 62
0

This should help. Create a BufferedReader object like this:

new BufferedReader(new FileReader("Location of the file"));

Read this doc, for reference: http://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html

Mark
  • 5,994
  • 5
  • 42
  • 55
ajay.patel
  • 1,957
  • 12
  • 15
  • thanks for the reply, i dnt wanna hard code the location of the file,else i want to get file from desktop by passing its name – Vipin Nair Jun 14 '13 at 11:48
  • Use Scanner class.Take file name as input String and append it to the location. String desktop = "C:\Users\xyz\Desktop\"; String inputFile = get it from scanner by calling scanner.nextString(). do String input = desktop + inputFile and pass this input to the constructor. – ajay.patel Jun 14 '13 at 12:00
0

You can try the following example to read file character basis.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package io;

import java.io.*;

/**
*
* @author Nandeshwar
*/
public class FileExample {

 public static void main(String[] args) {
    File inFile = new File("c:/users/toshiba/desktop/MyText.txt");

    FileReader ins = null;

    try {
        ins = new FileReader(inFile);

        int ch;
        while ((ch = ins.read()) != -1) {
            System.out.println((char) ch);

        }
    } catch (Exception e) {
        System.out.println(e);
    } finally {
        try {
            ins.close();
        } catch (Exception e) {
        }
    }
 }

}
suvartheec
  • 3,484
  • 1
  • 17
  • 21
Nandeshwar
  • 94
  • 1
  • 9
  • thanks for the reply,but how can i read content from a file from desktop by only giving its name,rather than giving hard coded full file name – Vipin Nair Jun 14 '13 at 11:49
  • String fileName = JOptionPane.showInputDialog("Enter file name"); File inFile = new File(fileName); Write these two lines , instead of File inFile = new.......... – Nandeshwar Jun 14 '13 at 11:55