how to read in a file in java
Java
// Java Program to illustrate reading from
// FileReader using FileReader
import java.io.*;
public class ReadingFromFile
{
public static void main(String[] args) throws Exception
{
// pass the path to the file as a parameter
FileReader fr =
new FileReader("C:\\Users\\pankaj\\Desktop\\test.txt");
int i;
while ((i=fr.read()) != -1)
System.out.print((char) i);
}
}
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files
public class ReadFile {
public static void main(String[] args) {
try {
File myObj = new File("filename.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
// Java Program to illustrate reading from FileReader
// using Scanner Class reading entire File
// without using loop
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadingEntireFileWithoutLoop
{
public static void main(String[] args)
throws FileNotFoundException
{
File file = new File("C:\\Users\\pankaj\\Desktop\\test.txt");
Scanner sc = new Scanner(file);
// we just need to use \\Z as delimiter
sc.useDelimiter("\\Z");
System.out.println(sc.next());
}
}
// Java Program to illustrate reading from FileReader
// using BufferedReader
import java.io.*;
public class ReadFromFile2
{
public static void main(String[] args)throws Exception
{
// We need to provide file path as the parameter:
// double backquote is to avoid compiler interpret words
// like \test as \t (ie. as a escape sequence)
File file = new File("C:\\Users\\pankaj\\Desktop\\test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null)
System.out.println(st);
}
}
Also in Java:
- Title
- how to format a datetime object to show date and time and milliseconds in java
- Category
- Java
- Title
- print in one line in java
- Category
- Java
- Title
- java fullscreen jframe
- Category
- Java
- Title
- most common element in list java
- Category
- Java
- Title
- java copy file from one directory to another efficiently
- Category
- Java
- Title
- creating thread in java example
- Category
- Java
- Title
- java.sql.SQLException: Before start of result set
- Category
- Java
- Title
- how to install java runtime environment on centos 7
- Category
- Java
- Title
- floor in java
- Category
- Java
- Title
- java integer compareto
- Category
- Java
- Title
- declare class variable in java
- Category
- Java
- Title
- How tomake teris in Java
- Category
- Java
- Title
- how to see if a shape is touching another shape in java
- Category
- Java
- Title
- imageview.setbackground
- Category
- Java
- Title
- add a value to a list java in java hashmap
- Category
- Java
- Title
- java function for power
- Category
- Java
- Title
- get long from date java
- Category
- Java
- Title
- java check if string is number
- Category
- Java
- Title
- null pointer exception java
- Category
- Java
- Title
- hide element selenium
- Category
- Java
- Title
- how to do a linear searc in java
- Category
- Java
- Title
- java character for end of file
- Category
- Java
- Title
- connecting to h2 database from java
- Category
- Java
- Title
- como saber de que tipo es una variable en java
- Category
- Java
- Title
- java instantiate collection with values
- Category
- Java
- Title
- pattern.compile java
- Category
- Java
- Title
- java for increment by 2
- Category
- Java
- Title
- how to get array input in java
- Category
- Java
- Title
- replace regex group() java
- Category
- Java
- Title
- integer max value java
- Category
- Java
- Title
- add two numbers bitwise
- Category
- Java
- Title
- java read file text
- Category
- Java
- Title
- check if a list contains a string java
- Category
- Java
- Title
- java if
- Category
- Java
- Title
- capitalize string java
- Category
- Java
- Title
- creating the functional interface in java
- Category
- Java
- Title
- how to create a draw Rectangle in java
- Category
- Java
- Title
- Java arraylist if you don't want to use add()
- Category
- Java
- Title
- firebase connecten
- Category
- Java
- Title
- open an existing excel file in java apache poi
- Category
- Java
- Title
- java - get open ports
- Category
- Java
- Title
- java clone method
- Category
- Java
- Title
- best companies to workk in
- Category
- Java
- Title
- java hashmap get value
- Category
- Java
- Title
- how to use pow function in java
- Category
- Java
- Title
- java 8 string to localdate
- Category
- Java
- Title
- jsonobject to java object
- Category
- Java
- Title
- run spring boot application command line
- Category
- Java
- Title
- diagonal difference hackerrank solution in java 8 using list
- Category
- Java
- Title
- how to substring in java
- Category
- Java
- Title
- java round double to 2 decimal places
- Category
- Java
- Title
- long to int java 8
- Category
- Java
- Title
- how to make a pre set list java
- Category
- Java
- Title
- exponents java
- Category
- Java
- Title
- a recursive function that calculates the greatest common divisor from user's input in java
- Category
- Java
- Title
- java read public key from keystore
- Category
- Java
- Title
- loop and save letters in a string java
- Category
- Java
- Title
- terminate a thread java
- Category
- Java
- Title
- one line if statement java
- Category
- Java
- Title
- how to create a Rectangle in java
- Category
- Java
- Title
- raspberry stackexchange how to install the java jdk
- Category
- Java
- Title
- taking string input in java
- Category
- Java
- Title
- 2d arrays | java
- Category
- Java
- Title
- Jlabel icon
- Category
- Java
- Title
- download spring
- Category
- Java
- Title
- array declaration and initialization in java
- Category
- Java
- Title
- processing library cassette
- Category
- Java
- Title
- waht does&& mean in java
- Category
- Java
- Title
- phoneword converter
- Category
- Java
- Title
- break for loop java
- Category
- Java
- Title
- code to include layout from java in android
- Category
- Java
- Title
- java resultset to table
- Category
- Java
- Title
- how to compare two maps in java
- Category
- Java
- Title
- kotlin jsonobject get nested
- Category
- Java
- Title
- get current unix timestamp java
- Category
- Java
- Title
- Java.awt graphics tutorial
- Category
- Java
- Title
- mock ParameterizedTypeReference
- Category
- Java
- Title
- how to make a char uppercase in java
- Category
- Java
- Title
- android java retrofit offline cache
- Category
- Java
- Title
- scan a string using scanner class in java
- Category
- Java
- Title
- how to print each element of an arraylist on a new line in java
- Category
- Java
- Title
- how to byheart faster
- Category
- Java
- Title
- how to declare array java
- Category
- Java
- Title
- java 8 remove spaces from string
- Category
- Java
- Title
- read from elasticsearch in spark
- Category
- Java
- Title
- how to make a variable unchangeable in java
- Category
- Java
- Title
- java get first char
- Category
- Java
- Title
- méthode retourne nom classe java
- Category
- Java
- Title
- recursion in java
- Category
- Java
- Title
- print values of bst java
- Category
- Java
- Title
- check if map contains key java
- Category
- Java
- Title
- mutable string in java
- Category
- Java
- Title
- how to add a keylistener to a jframe
- Category
- Java
- Title
- java switch
- Category
- Java
- Title
- Fragment DashboardFragment not associated with a fragment manager.
- Category
- Java
- Title
- java save string in text file
- Category
- Java
- Title
- StringBuilder java pop back
- Category
- Java
- Title
- print hello world in java
- Category
- Java
- Title
- if statement java
- Category
- Java
- Title
- javafx textarea font size
- Category
- Java