how to open a text file in java
Java
// Java Program to illustrate reading from Text File
// using Scanner Class
import java.io.File;
import java.util.Scanner;
public class ReadFromFileUsingScanner
{
public static void main(String[] args) throws Exception
{
// pass the path to the file as a parameter
File file =
new File("C:\\Users\\pankaj\\Desktop\\test.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine())
System.out.println(sc.nextLine());
}
}
// 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 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);
}
}
// Java program to illustrate reading data from file
// using nio.File
import java.util.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.io.*;
public class ReadFileIntoList
{
public static List<String> readFileInList(String fileName)
{
List<String> lines = Collections.emptyList();
try
{
lines =
Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
}
catch (IOException e)
{
// do something
e.printStackTrace();
}
return lines;
}
public static void main(String[] args)
{
List l = readFileInList("C:\\Users\\pankaj\\Desktop\\test.java");
Iterator<String> itr = l.iterator();
while (itr.hasNext())
System.out.println(itr.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
- create an empty array in java
- Category
- Java
- Title
- split every character in string into array java
- Category
- Java
- Title
- find the day of the week Calendar class in java
- Category
- Java
- Title
- switch en java
- Category
- Java
- Title
- programa que convierete un archi de c a java
- Category
- Java
- Title
- file with line numbers inserted java
- Category
- Java
- Title
- spigot title
- Category
- Java
- Title
- fibonacci sequence in java recursion
- Category
- Java
- Title
- java loop hashmap
- Category
- Java
- Title
- how to remove all items from alist in java
- Category
- Java
- Title
- difference between print and println in java
- Category
- Java
- Title
- java for each
- Category
- Java
- Title
- initialize scanner java
- Category
- Java
- Title
- java animated gif example
- Category
- Java
- Title
- get preference value android
- Category
- Java
- Title
- sdkmanager JAVA_HOME invalid directory
- Category
- Java
- Title
- java regex replace all characters before
- Category
- Java
- Title
- convert string to array java
- Category
- Java
- Title
- read csv in java in spring
- Category
- Java
- Title
- compile java
- Category
- Java
- Title
- java map declaration
- Category
- Java
- Title
- how to init an array with objects in java
- Category
- Java
- Title
- java how to initialize an array
- Category
- Java
- Title
- how to format a double in java to 2 decimal places
- Category
- Java
- Title
- calculate days between two dates in java
- Category
- Java
- Title
- java get html from url
- Category
- Java
- Title
- send variable intent
- Category
- Java
- Title
- capcitor FERR_CLEARTEXT_NOT_PERMITTED
- Category
- Java
- Title
- get first character of string java
- Category
- Java
- Title
- java string format .2f
- Category
- Java
- Title
- how to make a text field required in android studio
- Category
- Java
- Title
- recursive function for fibonacci series in java javascript
- Category
- Java
- Title
- open an existing excel file in java apache poi
- Category
- Java
- Title
- java http client eviction
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- enum java
- Category
- Java
- Title
- java string split from input string
- Category
- Java
- Title
- java read integer from text file into array scanner
- Category
- Java
- Title
- compareto method java
- Category
- Java
- Title
- FORM EN JAVA SCRIPT
- Category
- Java
- Title
- selection sort in java
- Category
- Java
- Title
- how to loop trough an object java script
- Category
- Java
- Title
- java random numbers in specific range
- Category
- Java
- Title
- java do while
- Category
- Java
- Title
- java fx display simple rectangle
- Category
- Java
- Title
- link to method javadoc
- Category
- Java
- Title
- java if different ways
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- java final meaning
- Category
- Java
- Title
- java delay
- Category
- Java
- Title
- java make arraylist
- Category
- Java
- Title
- how to get the width and height of a string in java
- Category
- Java
- Title
- replaceall in java
- Category
- Java
- Title
- how to parse a string into a number in java
- Category
- Java
- Title
- comparable on a generic class java
- Category
- Java
- Title
- android notification addaction example
- Category
- Java
- Title
- shorthand if java without else
- Category
- Java
- Title
- parseints(str) java
- Category
- Java
- Title
- java define main function
- Category
- Java
- Title
- binary numbers java
- Category
- Java
- Title
- for loop java
- Category
- Java
- Title
- money value commas java
- Category
- Java
- Title
- covariant type in java
- Category
- Java
- Title
- online money transfer andhra bank
- Category
- Java
- Title
- java string indexof
- Category
- Java
- Title
- java random number
- Category
- Java
- Title
- java find if element of list in present in another list
- Category
- Java
- Title
- how to change tablayout current view position in android
- Category
- Java
- Title
- parse string to int java
- Category
- Java
- Title
- get current day java
- Category
- Java
- Title
- how to add element to arraylist
- Category
- Java
- Title
- java get command line output
- Category
- Java
- Title
- java method overriding
- Category
- Java
- Title
- how to get elements of a list in java
- Category
- Java
- Title
- remove item from arraylist in java
- Category
- Java
- Title
- how to create a method java
- Category
- Java
- Title
- java string to double
- Category
- Java
- Title
- android hide and show bottom navigation
- Category
- Java
- Title
- android studio remove navigation bar
- Category
- Java
- Title
- how to crate a list in java script
- Category
- Java
- Title
- array in line java
- Category
- Java
- Title
- math minimum java
- Category
- Java
- Title
- how to find length of array in java
- Category
- Java
- Title
- how to skip a line in java
- Category
- Java
- Title
- how to add strings in java
- Category
- Java
- Title
- java get folder content
- Category
- Java
- Title
- restart application programmatically android
- Category
- Java
- Title
- Java for loop
- Category
- Java
- Title
- python to java converter
- Category
- Java
- Title
- how to add an image to a gui in java windowbuilder
- Category
- Java
- Title
- spigot disable join message
- Category
- Java
- Title
- java arraylist
- Category
- Java
- Title
- how to print ascii value in java
- Category
- Java
- Title
- javafx fill tableview with data
- Category
- Java
- Title
- goodbye java
- Category
- Java
- Title
- count the number of words in a string java
- Category
- Java
- Title
- java windowbuilder full screen
- Category
- Java
- Title
- onbackpressed android
- Category
- Java
- Title
- java logical operator
- Category
- Java
- Title
- java iterate over a string
- Category
- Java