0

This might be a weird question, but I really don't know how to ask it. I want to get any information logged into a file and make it accessible (e.x - a program logs into the file, and I get that value as a String in my project).

  • 2
    I was afraid to continue reading after "This might be a weird question, but I really don't know how to ask it." lolz – Tucker Mar 16 '13 at 03:52
  • Can you please provide more details, and any code that you have tried? – Sudhanshu Umalkar Mar 16 '13 at 03:52
  • This is a very broad question. You probably want to search Google for something like "java read input from file", try something, and come back with a more specific question when you get stuck. Welcome to SO! – knoight Mar 16 '13 at 03:54

2 Answers2

0

Using java.util.Scanner, you can read a txt file and add it's contents to a string.

public static void main(String[] args) throws FileNotFoundException {
  File source = new File("filename.txt");
  Scanner scanner = new Scanner(source);
  String myString = "";
  while(scanner.hasNext()) {
    myString = scanner.nextLine() + " ";
  }
    scanner.close()
 }
Faahmed
  • 375
  • 4
  • 21
0

Please provide more details about what you want to do? If you just need to read the log file then you can use Reader/Writer API in java to read/write text files.

Use BufferedReader class, which has readLine() method to read entire line at once. Sample program to read a file using BufferedReader class

Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38