Using information contained in text files - java

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();
}
}
}
}
}
}

Related

Best way to read data from 16MB file without freezing the app

I have a problem and can't find a good solution to it. I need to read a textfile with large amount of data (file has 16MB). The file contains 12 columns with integer values in each one. Generally my problem is how to do this without freezing the app. I have my file in the assets folder of my project and I tried using something like this:
AssetManager assetManager = this.getAssets();
try {
InputStream inputStream = assetManager.open("3333.ecg");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String str;
while ((str = bufferedReader.readLine()) != null) {
stringBuilder.append(str);
}
} catch (IOException e) {
e.printStackTrace();
}
But app freezes. My goal is to get the data from each column and save it into an arraylist of integers. I'm looking for some advice.
Thanks in advance.
Use this to run it in background:
AsyncTask.execute(new Runnable() {
#Override
public void run() {
AssetManager assetManager = this.getAssets();
try {
InputStream inputStream = assetManager.open("3333.ecg");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String str;
while ((str = bufferedReader.readLine()) != null) {
stringBuilder.append(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});

Get content from a text file to String (Android) (FileNotFound) (Java)

I want to get the text from a text file I have in my project (android studio) and make that text to a string. I am currently having trouble getting the correct path or something. I'm using two methods I found here on Stackoverflow to get the textfiles to Strings. These are the methods:
public static String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
return sb.toString();
}
public static String getStringFromFile (String filePath) throws Exception {
File fl = new File(filePath);
FileInputStream fin = new FileInputStream(fl);
String ret = convertStreamToString(fin);
//Make sure you close all streams.
fin.close();
return ret;
}
And I'm calling the methods like this, and I have tried all kinds of pathing but none seems to work:
Log.i("er0r", Solve.getStringFromFile("\\tables\\lowerLayer\\cross\\whiteRed.txt"));
This is just an attempt to print the content of the textfile. I get the following error: java.io.FileNotFoundException: .\tables\lowerLayer\cross\whiteRed.txt: open failed: ENOENT (No such file or directory)
This is how I have ordered my packages:
http://imgur.com/a/rK9R5
How can i fix this? Thanks
EDIT:
public String LoadData(String inFile) {
String str = "";
try{
StringBuilder buf=new StringBuilder();
InputStream json=getAssets().open(inFile);
BufferedReader in=
new BufferedReader(new InputStreamReader(json, "UTF-8"));
while ((str=in.readLine()) != null) {
buf.append(str);
}
in.close();
} catch (Exception e) {
Log.e("er0r", e.toString());
}
return str;
}
Tried this with inFile = "assets\whiteRed.txt"
Got me this error: java.io.FileNotFoundException: assets\whiteRed.txt
ADDITIONAL CODE:
Constructor of the class that's calling the LoadData method
public class Solve {
private Context context;
//constructor
public Solve(Context context){
this.context = context;
}
If at design time, in Android Studio, you want to supply files which your app can read at run time then put them in the assets folder of your Android Studio project.
Maybe you have to create that assets folder first.
After that your app can read those files from assets using assets manager.
Just google for how to do this exactly. All has been posted here many times.

Taking data from one text file and moving it to a new text file

I have a file that has data inside of it. In my main method I read in the file and closed the file. I call another method that created a new file inside of the same folder of the original file. So now I have two files, the original file and the file that is being made from the method that I call. I need another method that takes the data from the original file and writes it to the new file that is created. How do I do that?
import java.io.*;
import java.util.Scanner;
import java.util.*;
import java.lang.*;
public class alice {
public static void main(String[] args) throws FileNotFoundException {
String filename = ("/Users/DAndre/Desktop/Alice/wonder1.txt");
File textFile = new File(filename);
Scanner in = new Scanner(textFile);
in.close();
newFile();
}
public static void newFile() {
final Formatter x;
try {
x = new Formatter("/Users/DAndre/Desktop/Alice/new1.text");
System.out.println("you created a new file");
} catch (Exception e) {
System.out.println("Did not work");
}
}
private static void newData() {
}
}
If your requirement is to copy your original files content to new file. Then this may be a solution.
Solution:
First, read to your original file using BufferedReader and pass your content to another method which creates new file using PrintWriter. and add your content to your new file.
Example:
public class CopyFile {
public static void main(String[] args) throws FileNotFoundException, IOException {
String fileName = ("C:\\Users\\yubaraj\\Desktop\\wonder1.txt");
BufferedReader br = new BufferedReader(new FileReader(fileName));
try {
StringBuilder stringBuilder = new StringBuilder();
String line = br.readLine();
while (line != null) {
stringBuilder.append(line);
stringBuilder.append("\n");
line = br.readLine();
}
/**
* Pass original file content as string to another method which
* creates new file with same content.
*/
newFile(stringBuilder.toString());
} finally {
br.close();
}
}
public static void newFile(String fileContent) {
try {
String newFileLocation = "C:\\Users\\yubaraj\\Desktop\\new1.txt";
PrintWriter writer = new PrintWriter(newFileLocation);
writer.write(fileContent);//Writes original file content into new file
writer.close();
System.out.println("File Created");
} catch (Exception e) {
e.printStackTrace();
}
}
}

How to make BufferedRead() class read in folder?

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());
}
}
}

read multiple data in multiple files

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.

Categories