I'm trying to read and display the content of a group of txt files in specific folder with Java. I'm getting error in line with BufferedRead class. What I'm doing wrong?
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
public class DirectoryShow {
public static void main(String[] args) throws IOException {
File f = new File("D:\\Files"); // current directory
File[] files = f.listFiles();
for (File file : files) {
BufferedReader br = new BufferedReader("D:\\Files");
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.print(" file:");
System.out.println(file.getCanonicalPath());
}
}
}
There are two errors:
The first is that where you wrote
BufferedReader br = new BufferedReader("D:\\Files");
that doesn't specify a single file; you probably mean
new BufferedReader(new InputStreamReader(new FileInputStream(file)));
The second error is that it might not the case that every file in the folder is a file, is accessible for reading, etc. In a well-designed application you should check for those things.
public class DirectoryShow {
public static void main(String[] args) throws IOException {
File f = new File("D:\\Files"); // current directory
File[] files = f.listFiles();
for (File file : files) {
BufferedReader br = new BufferedReader(new InputstreamReader(new FileInpupStream(file)));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
System.out.print(" file:");
System.out.println(file.getCanonicalPath());
}
}
}
Related
I have a problem with the task and I would like to ask for tips.
There are three text files with the names: doctors.txt, patients.txt, visits.txt.
They contain information about doctors, patients and home visits.
In each of the files, the data in the line are separated by tabs.
Using the information contained in the files, execute the following commands:
find the doctor who has had the most visits.
I am having trouble reading these files. How do I do this to convert the data from these three text files?
I created the doctor, patient, visit, time classes and in the main I put the files into the blackboard.
enter code here
public static void main(String[] args) throws IOException {
File[] files = {new File("doctors.txt"), new File("patients.txt"),
new File("visits.txt")};
for (File file : files) {
if(file.isFile()) {
BufferedReader inputStream = null;
String line;
try {
inputStream = new BufferedReader(new FileReader(file));
while ((line = inputStream.readLine()) != null) {
System.out.println(line);
}
}catch(IOException e) {
System.out.println(e);
}
finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}
}
}
I can't wrap my head around why I get zero output... The code looks correct to me, and it compiles with no problem (except for the lack of output). I have tried with absolute path. The text file is stored in the same folder as the class. Am I missing something obvious?
public class File {
public static void main(String[] args) throws FileNotFoundException {
String filename = "./inputD2.txt";
readFile(filename);
System.out.println( readFile(filename));
}
private static List<String> readFile(String filename) {
List<String> records = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) {
records.add(line);
}
reader.close();
return records;
}
catch (Exception e) {
System.err.format("Exception occurred trying to read '%s'.", filename);
e.printStackTrace();
return null;
}
}
}
package com.test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
public class FileReaderTest {
public static void main(String[] args) {
String filename = "F:\\Sixth_workspace\\Sampleproject\\src\\main\\resources\\try.txt";
System.out.println("Reading from the text file" + " " + readFile(filename));
}
private static List<String> readFile(String filename) {
List<String> records;
try {
records = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) {
records.add(line);
}
reader.close();
return records;
} catch (Exception e) {
System.err.println("Exception occurred trying to read '%s'." + filename);
e.printStackTrace();
return null;
}
}
}
I modified your code and got the desired output. Use the full path of the text file, here
F:\\Sixth_workspace\\Sampleproject\\src\\main\\resources\\try.txt
is my full path.
Changes:
Changed the classname
Given full path of the text file
Using java 1.8 (above 1.5 is required)
How to read from a file strings or numbers with one space between them.
You can just write a class which will read a file and remove the spaces.
package com.example.removespaces
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class RemoveSpacesInFileEx {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("outfile.txt");
String line;
while((line = br.readLine()) != null)
{
line = line.trim();
line=line.replaceAll("\\s+", " ");
fw.write(line);
}
fr.close();
fw.close();
}
}
public void readData() {
File file = new File("file.txt"); //You can put your file name or your file position here
try {
Scanner sc = new Scanner(file);
while(sc.hasNext()) {
System.out.println(sc.nextLine());
} System.out.println();
}
catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
System.out.println("End of the Main");
} //This will read it from a file and print on your console
I have this FileIO class which reads a .txt file and stores them into a String ArrayList. However when i tried to print out the contents of my arrayList, it appears to be empty. Where have i made an error?
public class FileIO
{
public ArrayList<String> readFile() throws IOException
{
ArrayList<String> al = new ArrayList<String>();
try
{
File file = new File("example.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null)
{
al.add(line);
}
fileReader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println();
for (int i = 0; i < al.size(); i++)
{
System.out.println(al.get(i));
}
return al;
}
}
public class Main
{
public static void main(String[] args)
{
FileIO fileIO = new FileIO();
ArrayList<String> temp = fileIO.readFile();
}
}
The contents of my txt file is just:
this is text1
this is text2
this is text3
Most probable reason for not getting data is that you haven't set the file path correctly. And below line is not necessary for this.
File file = new File("example.txt");
You can directly create a FileReader object from the file name as below.
FileReader fileReader = new FileReader("example.txt);
I have a list of files in the directory C:\Users\Mahady\Desktop\Java 31122011\src\register\
they are like this....
100100545.txt
100545454.txt etc etc
in each file, file data are like this line by line:
Bob
1234
4834
London
9852
1
My question is, how do i read each files one by one in the directory and for each files read all lines except line 3. i would then like to merge this data in word and create letters. thanks
Detailed Answer....
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileRead {
public static void main(String[] args) {
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try {
File folder = new File("C:/Users/Mahady/Desktop/Java 31122011/src/register/");
if (folder.isDirectory()) {
for (File file : folder.listFiles()) {
fileReader = new FileReader(file);
bufferedReader = new BufferedReader(fileReader);
String line = null;
int lineCount = 0;
while (null != (line = bufferedReader.readLine())) {
lineCount++;
if (3 != lineCount) {
System.out.println(line);
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != bufferedReader)
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Hope this would help you.
Try this:
File dir = new File("C:\\Users\\Mahady\\Desktop\\Java 31122011\\src\\register\\");
for (string fn : dir.list()) {
FileInputStream fstream = new FileInputStream(fn);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
System.out.println (strLine);
}
in.close();
}
Obviously, you will need to add exception handling code around this skeletal implementation.