开发者

related to java file handling

开发者 https://www.devze.com 2023-01-01 06:21 出处:网络
In Java how can I take the data of my file o开发者_运维技巧n my display screen? I want to use data in my file and also want that data to be displayed on my output screen when I execute my program. Can

In Java how can I take the data of my file o开发者_运维技巧n my display screen? I want to use data in my file and also want that data to be displayed on my output screen when I execute my program. Can any body please help by providing me such example in Java language. Thank you!


This is an "I/O" topic (input/output). The related classes are in package java.io.

If you're reading a simple text file, java.util.Scanner can be very useful. There are many examples in the documentation, and also elsewhere on StackOverflow.

See also

  • Java Lessons/Basic I/O

Simple example

The following code takes a filename from the command line, treating it as a text file, and simply print its content to standard output.

import java.util.*;
import java.io.*;
public class FileReadSample {
    public static void main(String[] args) throws FileNotFoundException {       
        String filename = args[0]; // or use e.g. "myFile.txt"
        Scanner sc = new Scanner(new File(filename));
        while (sc.hasNextLine()) {
            System.out.println(sc.nextLine());
        }
    }
}

You can compile this, and then run it as, say, java FileReadSample myFile.txt.

For beginners, Scanner is recommended, since it doesn't require complicated handling of IOException.

API links

  • Scanner.hasNextLine()
    • Returns true if there is another line in the input of this scanner.
  • Scanner.nextLine()
    • Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
  • Scanner.ioException()
    • Returns the IOException last thrown by this Scanner's underlying Readable. This method returns null if no such exception exists.

See also

  • Java Lessons/Basic I/O/Scanning and formatting
  • Java Tutorials/Command Line Arguments


A modified version of this example:

import java.io.*;
class Demo {
    public static void main( String [] args ) {
        try { 
            BufferedReader in = new BufferedReader(new FileReader("youFile.txt")); 
            String str; 
            while ((str = in.readLine()) != null) { 
                System.out.println( str ); 
            } 
            in.close(); 
        } catch (IOException e) {}     
    }
}

This is not precisely a "best practice" demo ( ie You should not ignore exceptions) , just what you needed: take data from file and display it on the screen"

I hope you find it helpful.


// Create

Path file = ...;
try {
    file.createFile();   //Create the empty file with default permissions, etc.
} catch (FileAlreadyExists x) {
    System.err.format("file named %s already exists%n", file);
} catch (IOException x) {
    //Some other sort of failure, such as permissions.
    System.err.format("createFile error: %s%n", x);
}

// Read

Path file = ...;
InputStream in = null;
try {
    in = file.newInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String line = null;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
} catch (IOException x) {
    System.err.println(x);
} finally {
    if (in != null) in.close();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消