Q1:-How to read a text file in Java?
ans:-
example:-public class FileReaderDemo{
ans:-
Solution 1 - Reading File using Scanner:-
Once you imported this class, you can create object of Scanner by passing a FileInputStream to it, pointing to the file you want to read
Scanner provides a method called hasNextLine() which returns true if file has one more line to read.
public static void main(String args[]) throws IOException
{
final String FILE_NAME = "C://temp//xyz.txt";
// 1st way to read File in Java - Using
Scanner scnr = new Scanner(new FileInputStream(FILE_NAME));
while (scnr.hasNextLine()) {
System.out.println(scnr.nextLine());
}
scnr.close();
}
}
}
}
// 2nd way to read File in Java - Using BufferedReader BufferedReader buffReader = new BufferedReader(new InputStreamReader(new FileInputStream(FILE_NAME)));
Solution 2 - Reading File using BufferedReader:-
ans:-how to read a file using BufferedReader class. Both classes are defined in java.util package so you need to import them before using it
InputStreamReader actually acts as a bridge between streams and reader classes. Once you have an object of BufferedReader, you can call readLine() method to read the next line from file.
example:-public class FileReaderDemo{
public static void main(String args[]) throws IOException
{
final String FILE_NAME = "C://temp//xyz.txt";
// 2nd way to read File in Java - Using BufferedReader BufferedReader buffReader = new BufferedReader(new InputStreamReader(new FileInputStream(FILE_NAME)));
String line = buffReader.readLine();
while (line != null) {
System.out.println(line);
line = buffReader.readLine();
}
}
}
Q-How to read XLS and XLSX file in Java?
Q-How to read XLS and XLSX file in Java?
ans:-
InputStream inputWorkbook1 = Injecting.class.getClassLoader().getResourceAsStream("data/piyush.xls");
BufferedReader br = new BufferedReader(new InputStreamReader(inputWorkbook1));
System.out.println("i am in inside file");
Workbook w;
int arrayLength = 0;
int i = 0;
try {
w = Workbook.getWorkbook(inputWorkbook1);
// Get the first sheets
Sheet sheet = w.getSheet(0);
// Loop over first 10 column and lines
Cell[] cell0 = sheet.getColumn(0);
Cell[] cell1 = sheet.getColumn(1);
Cell[] cell2 = sheet.getColumn(2);
System.out.println(userId + "cell0 --> " + cell0[0].getContents()
+ "\ncell1 -- >" + cell1[0].getContents() + "\ncell2 -- > "
+ cell2[0].getContents());
InputStream inputWorkbook1 = Injecting.class.getClassLoader().getResourceAsStream("data/piyush.xls");
BufferedReader br = new BufferedReader(new InputStreamReader(inputWorkbook1));
System.out.println("i am in inside file");
Workbook w;
int arrayLength = 0;
int i = 0;
try {
w = Workbook.getWorkbook(inputWorkbook1);
// Get the first sheets
Sheet sheet = w.getSheet(0);
// Loop over first 10 column and lines
Cell[] cell0 = sheet.getColumn(0);
Cell[] cell1 = sheet.getColumn(1);
Cell[] cell2 = sheet.getColumn(2);
System.out.println(userId + "cell0 --> " + cell0[0].getContents()
+ "\ncell1 -- >" + cell1[0].getContents() + "\ncell2 -- > "
+ cell2[0].getContents());
No comments:
Post a Comment