Unable to Read a Text File via BufferedReader - java

I am unable to read a text file via a BufferedReader Object. However I can successfully write to the same text file via a BufferedWriter Object.
The intent of my program is to read a text file called queryCountFile.txt which will then able to figure out how many Query Objects (my custom object) have been previously created. From there, I will then be able to create as many Query Objects as a want while being able to keep track of the number of said Queries.
The function that tries (and fails) to read from the text file is called findNumberOfQueries(). It is located in my file called Query.java
Can anyone understand why I am unable to read from the text file?
QuickControllerApplication.java
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class QuickControllerApplication {
public static void main(String[] args) {
SpringApplication.run(QuickControllerApplication.class, args);
//everthing below this line is for testing purposes
Query littleQuery = new Query(101L);
//littleQuery.testPrint();
littleQuery.generateQueryID();
System.out.println(littleQuery.findNumberOfQueries());
}
}
Query.java
/**************************************************************
* queryIDNumber - a long that holds the individual data of an
* individual query. Each query will have a unique number
* associated with it.
**************************************************************/
public class Query {
final Long MIN_ID_NUMBER = 1L; //minimum ID Number that can be ever generated by the program
final String QUERY_COUNT_FILE = "queryCountFile.txt"; //this file will simply hold a number that states how many queries have been created
final int SKIP_NUM_LINES_IN_FILE = 2; //the first X number of lines that will skipped in QUERY_COUNT_FILE
//final Long MAX_ID_NUMBER = 9223372036854775807L; //maximum ID Number that can be ever generated by the program
private Long queryID; //each query must have a unique ID Number which will go in the URL when the user is viewing the result of the query search.
private static Long totalQueryIDs = 0L; //holds the value of how many queries have been created over the life of the program
public Query(Long previouslyGeneratedIDNumber)
{
generateQueryID();
//totalQueryIDs++;
//OTHER FUNCTION CALLS
//WILL PROBABLY GO
//HERE LATER...
}
/**************************************************************
* generateQueryID - Generate a ID Number for a query. ID
* Number must be unique, and then is assigned to queryID
**************************************************************/
public void generateQueryID(){
Long generatedNumber;
//Finds the totalQueryIDs stored in QUERY_COUNT_FILE
generatedNumber = findNumberOfQueries();
if (generatedNumber <= MIN_ID_NUMBER){
totalQueryIDs = MIN_ID_NUMBER;
}
else {
totalQueryIDs = generatedNumber + 1L;
}
queryID = totalQueryIDs;
}
/**************************************************************
* findNumberOfQueries - This function finds out how many
* queries have been generated so far. This function will check
* a text file that will contain the past number of queries
* that have been generated.
**************************************************************/
public Long findNumberOfQueries(){
//Check a file. If queryCountFile.txt is not found then numberOfQueries is considered 0 and becomes 1?
try {
Date date = new Date();
//Assume default encoding.
FileWriter fileWriter = new FileWriter(QUERY_COUNT_FILE);
FileReader fileReader = new FileReader(QUERY_COUNT_FILE);
//Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
//Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
bufferedWriter.write("FILE LAST WRITTEN TO ON: " + date + "\n");
bufferedWriter.write("totalQueryIDs:\n");
bufferedWriter.write("5");
//reading from QUERY_COUNT_FILE
try{
System.out.println("got here\n"); //debug statement
String line; //temp var
//skip first SKIP_NUM_LINES_IN_FILE lines in QUERY_COUNT_FILE
for (int count = 0; count < SKIP_NUM_LINES_IN_FILE; count++) {
bufferedReader.readLine();
}
line = bufferedReader.readLine();
while (line != null) {
System.out.println("stuff bufferedReader got: " + line);
}
}
catch(IOException ex) {
System.out.println("Error reading to file '" + QUERY_COUNT_FILE + "'");
}
//Close the file.
bufferedWriter.close();
bufferedReader.close();
}
catch(IOException ex) {
System.out.println("Error writing to file '" + QUERY_COUNT_FILE + "'");
}
return totalQueryIDs;
}
}

Let me suggest you another way of reading your lines using most recent APIs method which make it easier to read and maintain (at least in my opinion) :
try(final Stream<String> fileStream = Files.lines(Paths.get("queryCountFile.txt")){
fileStream.skip(SKIP_NUM_LINES_IN_FILE)
.forEach(line -> processMyLine(line));
}
For completeness, the problem in your example is that you never re-assign line variable in your loop :
while(line != null)
should be :
while((line = bufferedReader.readLine()) != null)

Related

How to read and append text file based on certain conditions

What the program does: A user loads a file and it saves the file name, creation date and a file number to a text file. I want the program to check the file to see if the filename is already stored. If it is, to then check the creation date to see if it matches the one the user is currently trying to save. If it differs, to save over the currently saved creation date with the new one.
An example of Info.txt:
CreationDate: 140319, FileName: example1.txt, FileNumber: 1
CreationDate: 110219, FileName: example2.txt, FileNumber: 6
CreationDate: 100319, FileName: example3.txt, FileNumber: 14
How I create and write to the file:
public void fileCreation() throws IOException {
String fileinformation = File.creationdate + ", " + File.name + ", " + "Number: " + File.Number;
FileWriter wr = new FileWriter("Info.txt", true);
PrintWriter output = new PrintWriter(wr);
output.println(fileinformation);
output.close();
}
How I'm currently checking the file, at the moment it only prints the file contents, so if a User goes to save example2.txt with a different creation date, then the new one should overwrite the current one
public void scanFile() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("Info.txt"));
String readfile;
while ((readfile = br.readLine()) != null) {
System.out.println(readfile);
}
System.out.println("file not found");
}
Creation date is a long number and File.creationdate is a string such as "CreationDate: 140319", it would also be good to put a check to see if the FileNumber matches, but this is not necessary.
You should use split on your line with ',' as a separator.
Then you can go through every line and for each line check if first the name is the same as the file name, if it is, go and check the date and then finally check the FileNumber.
But why do you need to check it one by one ? Better to write it in with &&, like this
public boolean scanFile() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("Info.txt"));
String readfile;
Boolean isAlreadyIn = false;
while ((readfile = br.readLine()) != null) {
String[] parts = readFile.split(',');
if(parts[1].replace("FileName: ","").equals(File.name) &&parts[0].replace("CreationDate: ","").equals(File.creationdate) &&parts[2].replace("FileNumber: ","").equals(File.number){
isAlreadyIn = true; // you can also write directly in this fonction
}
return isAlreadyIn;
}
}
if you really need to check it one by one, just make a if(if( if()) with the condition in it

Why does running .jar take 2-3 times longer than the same .java files in the ide?

Over Easter I made a program that would take a large log file, read it in and parse it and output this to a table view with javafx. The program works and so made it into a .jar using Intellij so it could easily be used without an IDE.
However when running the program takes about 2-3 times longer when running the I/O methods in the JAR form of the program.
(.java file)3.5 seconds reading in -> (.jar file) 11.3 seconds
(.java file)0.014 seconds writing out -> (.jar file) 0.034 seconds
I have gone through reading the over older questions about this. Have tried timing the methods, making sure the latest JDK/JRE are being used.
The methods I am pretty sure are slowing down when run as a jar are:
/**
* Method that filters a file based on a search string and a selected radio button
*
* #param num index of a selected radio button
* #param filterSearch String that is being searched for
* #param path Of the log file that is being filtered through
* #return The new file that contains only the desired lines
*/
public static String filterFile(int num, String filterSearch, String path) {
long startTime = System.currentTimeMillis();
ArrayList<String> toOutput = new ArrayList<>();
Scanner in = null;
File inputFile = new File(path);
try {
in = new Scanner(inputFile);
String searchString = null;
//While the log file has a line
while (in.hasNext()) {
//the next line to be searched is stored in searchString
searchString = in.nextLine();
switch (num) {
//if the user radio button is selected
case 1:
//if the searchString contains the filter search
//print the whole line to the new file
if (searchString.contains("user=\"" + filterSearch)) {
//System.out.println(searchString);
toOutput.add(searchString);
}
break;
case 2:
//if the searchString contains the filter search
//print the whole line to the new file
if (searchString.contains("srcip=\"" + filterSearch)) {
toOutput.add(searchString);
}
break;
}
}
} catch (Exception e) {
System.out.println("Cannot open file: ");
System.exit(0);
}finally {
in.close();
}
long endTime = System.currentTimeMillis();
double duration = (endTime - startTime)/1000.0;
System.out.println("first formatedSeconds = "+ duration);
return createFilterFile(toOutput);
}
/**
* Create a new file with the filtered results inside
* #param toPrint lines from the log file that have been filtered out
* #return Path to this new file
*/
private static String createFilterFile(ArrayList<String> toPrint){
long startTime = System.currentTimeMillis();
Date date = new Date();
String fileName = "New filtered search file " + sdf.format(date) + ".log";
PrintWriter out = null;
try{
FileWriter outputFile = new FileWriter(fileName, true);
out = new PrintWriter(outputFile);
for(String item: toPrint){
out.println(item);
}
}catch(Exception e){
e.printStackTrace();
}finally {
out.close();
}
long endTime = System.currentTimeMillis();
double duration = (endTime - startTime)/1000.0;
// formatedSeconds = (0.xy seconds)
System.out.println("second formatedSeconds = "+ duration);
return fileName;
}
Edit added finally clause to both methods, didnt change any performance tho
This code takes the large log file, searches for each line for a searched string and adds it to an array list, then create a new file. This file is significantly smaller so can be easily opened.
Any help towards improving the performance of the jar would be appreciated and as I don't have much experience with .jar files!
EDIT: Changing From a scanner to a buffered reader drastically improved performance
(.java file)1.39 seconds reading in -> (.jar file) 1.82 seconds
(.java file)0.014 seconds writing out -> (.jar file) 0.034 seconds
/**
* Method that filters a file based on a search string and a selected radio button
*
* #param num index of a selected radio button
* #param filterSearch String that is being searched for
* #param path Of the log file that is being filtered through
* #return The new file that contains only the desired lines
*/
public static String filterFile(int num, String filterSearch, String path) {
long startTime = System.currentTimeMillis();
ArrayList<String> toOutput = new ArrayList<>();
BufferedReader reader = null;
File inputFile = new File(path);
try {
reader = new BufferedReader(new FileReader(inputFile));
String searchString = null;
//While the log file has a line
//the next line to be searched is stored in searchString
while ((searchString = reader.readLine())!=null) {
switch (num) {
//if the user radio button is selected
case 1:
//if the searchString contains the filter search
//print the whole line to the new file
if (searchString.contains("user=\"" + filterSearch)) {
toOutput.add(searchString);
}
break;
case 2:
//if the searchString contains the filter search
//print the whole line to the new file
if (searchString.contains("srcip=\"" + filterSearch)) {
toOutput.add(searchString);
}
break;
}
}
} catch (IOException e) {
System.out.println("Cannot open file: ");
System.exit(0);
}finally {
try{
reader.close();
}catch (IOException ef){
}
}
long endTime = System.currentTimeMillis();
double duration = (endTime - startTime)/1000.0;
System.out.println("first formatedSeconds = "+ duration);
return createFilterFile(toOutput);
}
Ofcourse there is still about half a second but this is not so noticeable if anyone can explain why there is such a drastic difference between scanner and buffered reader I would be appreciative.
Should I always use buffered reader?
Is buffered writer going to give a similar performance increase?
Why is there still a performance difference ?
Thanks everyone :)

Problems writing to and reading from a file

I am new to programming (Sorry if I ask an easy question) and I have a problem with my program dealing with writing to and reading from a file. To start off I ask the user what they want their username and password to be. Then to simply check if what I was doing was correct, I tried to read the file and then print out the same information. Here is my code:
public void createAccount()
{
try
{
FileWriter doc = new FileWriter("Username.ctxt", true);
System.out.print("Enter your desired Username: ");
myUsername = keyboard.next();
System.out.println();
System.out.print("Enter your desired Password: ");
myPassword = keyboard.next();
System.out.println();
String doc2 = myUsername + " " + myPassword + "\n";
doc.write(doc2, 0, doc2.length());
doc.close();
}
catch(IOException e)
{
System.out.println("Error: " + e.getMessage());
}
retrieveAccount();
}
public void retrieveAccount()
{
try
{
BufferedReader reader = new BufferedReader(new FileReader("Username.ctxt"));//
String user = new String("");//username
String pass = new String("");//password
int stop;
String line = null;
System.out.print("Enter your username: ");//allows computer to search through file and find username
username = keyboard.next();
while ((line = reader.readLine()) != null)
{
scan = reader.readLine();
stop = scan.indexOf(" ");
user = scan.substring(0, stop);
System.out.println(user);
pass = scan.substring(stop + 1);
System.out.println(pass);
if(user.equals(myUsername))
{
System.out.println("Your password is: " + pass);
break;
}
}
}
catch(IOException a)
{
System.out.println("Error: " + a.getMessage());
}
}
So what I want to happen is:
Enter desired username: jake101
Enter desired password: coolKid
Enter your username: jake101
your password is: coolKid
But what actually happens is, is and out of bounds exception(-1)
This is happening because when I use indexOf(" "); it searches for a space. And when it returns negative 1 it means there is no space. What i believe is happening is that i am not writing to the same document i am trying to read from. If anybody can help me figure out what i am doing wrong this would help!
You're double readling the contents of the file...
You first read a line from the file using...
while ((line = reader.readLine()) != null) {
The, straight after that, you read another line using...
String scan = reader.readLine();
Get rid of the second line read...
The issue is that you are calling readline twice in same loop
while ((line = reader.readLine()) != null)
{
scan = reader.readLine();
Change the above to following and it will work
while ((line = reader.readLine()) != null)
{
String scan = line;
The problem seems to be in your retrieveAccount() method, try closing ur reader object. U have opened the file in retrieveAccount() and never closed (so its stil under locked state for other applns/mthds/threads to access).
Try adding reader.close() before end of try block
I would suggest you to create seperate methods for createAccount,retrieveAccount,writeToFile and readToFile. A method should always be responsible to handle single modules. Is the actual responsibillity of createAccount method to read from a file? I would totally say no. Firstly, because low coupling - high cohesion principles are not followed and secondly, because reusabillity does not exist in this way. There are other issues that occur with your currect approach but since you are still in the beginning is expected.
I will provide you with some parts of the things you could do, however, there will be some parts that you should work on your own, like creating the User Class ( it shouldn't be difficult and it will help you learn)
So let's see.
public void createAccount(User user, ListInterface<User> userList)
throws AuthenticationException {
if (!userList.exists(user)) {
userList.append(user);
} else {
throw new AuthenticationException(
"You cannot add this user. User already exists!");
}
}
public boolean authenticate(User user, ListInterface<User> userList)
throws AuthenticationException {
for (int i = 1; i <= userList.size(); i++) {
if (user.equals(userList.get(i))
&& user.getPassword().equals(
userList.get(i).getPassword())) {
return true;
}
}
return false;
}
public void readFromFile(String fileName, ListInterface<User> userList) {
String oneLine, oneLine2;
User user;
try {
/*
* Create a FileWriter object that handles the low-level details of
* reading
*/
FileReader theFile = new FileReader(fileName);
/*
* Create a BufferedReader object to wrap around the FileWriter
* object
*/
/* This allows the use of high-level methods like readline */
BufferedReader fileIn = new BufferedReader(theFile);
/* Read the first line of the file */
oneLine = fileIn.readLine();
/*
* Read the rest of the lines of the file and output them on the
* screen
*/
while (oneLine != null) /* A null string indicates the end of file */
{
oneLine2 = fileIn.readLine();
user = new User(oneLine, oneLine2);
oneLine = fileIn.readLine();
userList.append(user);
}
/* Close the file so that it is no longer accessible to the program */
fileIn.close();
}
/*
* Handle the exception thrown by the FileReader constructor if file is
* not found
*/
catch (FileNotFoundException e) {
System.out.println("Unable to locate the file: " + fileName);
}
/* Handle the exception thrown by the FileReader methods */
catch (IOException e) {
System.out.println("There was a problem reading the file: "
+ fileName);
}
} /* End of method readFromFile */
public void writeToFile(String fileName, ListInterface<User> userList) {
try {
/*
* Create a FileWriter object that handles the low-level details of
* writing
*/
FileWriter theFile = new FileWriter(fileName);
/* Create a PrintWriter object to wrap around the FileWriter object */
/* This allows the use of high-level methods like println */
PrintWriter fileOut = new PrintWriter(theFile);
/* Print some lines to the file using the println method */
for (int i = 1; i <= userList.size(); i++) {
fileOut.println(userList.get(i).getUsername());
fileOut.println(userList.get(i).getPassword());
}
/* Close the file so that it is no longer accessible to the program */
fileOut.close();
}
/* Handle the exception thrown by the FileWriter methods */
catch (IOException e) {
System.out.println("Problem writing to the file");
}
} /* End of method writeToFile */
Useful Information:
The userList is a dynamic linked list that uses generics (ListInterface<User>)
if you dont want to use generics you could just say ListInterface userList, whereever it appears.
Your User class should implement the comparable and include the methods stated below:
public int compareTo(User user) {
}
public boolean equals(Object user) {
}
Always try to create "plug-an-play" methods(not hardcoded), that's the reason I pass as a parameter the userList.
Note that, in case that you dont use generics, typecast might be needed. Otherwise, you will get compilation errors.
If you have any questions let me know.

Read one line of a csv file in Java

I have a csv file that currently has 20 lines of data.
The data contains employee info and is in the following format:
first name, last name, Employee ID
So one line would like this: Emma, Nolan, 2
I know how to write to the file in java and have all 20 lines print to the console, but what I'm not sure how to do is how to get Java to print one specific line to the console.
I also want to take the last employee id number in the last entry and have java add 1 to it one I add new employees. I thinking this needs to be done with a counter just not sure how.
You can do something like this:
BufferedReader reader = new BufferedReader(new FileReader(<<your file>>));
List<String> lines = new ArrayList<>();
String line = null;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
System.out.println(lines.get(0));
With BufferedReader you are able to read lines directly. This example reads the file line by line and stores the lines in an array list. You can access the lines after that by using lines.get(lineNumber).
You can read text from a file one line at a time and then do whatever you want to with that line, print it, compare it, etc...
// Construct a BufferedReader object from the input file
BufferedReader r = new BufferedReader(new FileReader("employeeData.txt"));
int i = 1;
try {
// "Prime" the while loop
String line = r.readLine();
while (line != null) {
// Print a single line of input file to console
System.out.print("Line "+i+": "+line);
// Prepare for next loop iteration
line = r.readLine();
i++;
}
} finally {
// Free up file descriptor resources
r.close();
}
// Remember the next available employee number in a one-up scheme
int nextEmployeeId = i;
BufferedReader reader =new BufferedReader(new FileReader("yourfile.csv"));
String line = "";
while((line=reader.readLine())!=null){
String [] employee =line.trim().split(",");
// if you want to check either it contains some name
//index 0 is first name, index 1 is last name, index 2 is ID
}
Alternatively, If you want more control over read CSV files then u can think about CsvBeanReader that will give you more access over files contents..
Here is an algorithm which I use for reading csv files. The most effective way is to read all the data in the csv file into a 2D array first. It just makes it a lot more flexible to manipulate the data.
That way you can specify which line of the file to print to the console by specifying it in the index of the array and using a for. I.e: System.out.println(employee_Data[1][y]); for record 1. y is the index variable for fields. You would need to use a For Loop of course, to print every element for each line.
By the way, if you want to use the employee data in a larger program, in which it may for example store the data in a database or write to another file, I'd recommend encapsulating this entire code block into a function named Read_CSV_File(), which will return a 2D String array.
My Code
// The return type of this function is a String.
// The CSVFile_path can be for example "employeeData.csv".
public static String[][] Read_CSV_File(String CSVFile_path){
String employee_Data[][];
int x;
int y;
int noofFields;
try{
String line;
BufferedReader in = new BufferedReader(new FileReader(CSVFile_path));
// reading files in specified directory
// This assigns the data to the 2D array
// The program keeps looping through until the line read in by the console contains no data in it i.e. the end of the file.
while ( (( line = in.readLine()) != null ){
String[] current_Record = line.split(",");
if(x == 0) {
// Counts the number of fields in the csv file.
noofFields = current_Record.length();
}
for (String str : values) {
employee_Data[x][y] = str;
System.out.print(", "+employee_Data[x][y]);
// The field index variable, y is incremented in every loop.
y = y + 1;
}
// The record index variable, x is incremented in every loop.
x = x + 1;
}
// This frees up the BufferedReader file descriptor resources
in.close();
/* If an error occurs, it is caught by the catch statement and an error message
* is generated and displayed to the user.
*/
}catch( IOException ioException ) {
System.out.println("Exception: "+ioException);
}
// This prints to console the specific line of your choice
System.out.println(("Employee 1:);
for(y = 0; y < noofFields ; y++){
// Prints out all fields of record 1
System.out.print(employee_Data[1][y]+", ");
}
return employee_Data;
}
For reading large file,
log.debug("****************Start Reading CSV File*******");
copyFile(inputCSVFile);
StringBuilder stringBuilder = new StringBuilder();
String line= "";
BufferedReader brOldFile = null;
try {
String inputfile = inputCSVFile;
log.info("inputfile:" + inputfile);
brOldFile = new BufferedReader(new FileReader(inputfile));
while ((line = brOldFile.readLine()) != null) {
//line = replaceSpecialChar(line);
/*do your stuff here*/
stringBuilder.append(line);
stringBuilder.append("\n");
}
log.debug("****************End reading CSV File**************");
} catch (Exception e) {
log.error(" exception in readStaffInfoCSVFile ", e);
}finally {
if(null != brOldFile) {
try {
brOldFile.close();
} catch (IOException e) {
}
}
}
return stringBuilder.toString();

Reading a customers name in a txt file with File Reader

My problem is that I can't figure out how to read the customers name with File Reader.
I am making a reservation system and I need to know of the customer already exist. That's why I have to read my Customers.txt file so I can check if someone is already a customer. If he is not I will make a new one with File writer(I Already have the code).
The meaning of this reservation system is to make reservations with a barber. I have to put the reservations in another txt file called Reservations.txt , and in that file you can see each reservation time and who made the reservation.
Thanks for the help!
This is the code I already have:
(some comments are in Dutch but I will translate them )
package nielbutaye;
import java.io.*;
import java.util.UUID;
/**
* #author Niel
*
*/
public class Klant {
//declaration that represents the text
public static String S;
public static String NEWLINE = System.getProperty("line.separator");
/**
* constructor
*/
public Klant(){}
/**
* #return
* By giving the name of the customer you will get all the data from the customer
*/
public double getCustomer() {
return 0 ;
}
/**
* Make a new customer
*/
public void setNew customer(){
// make a newSimpleInOutDialog
SimpleInOutDialog input = new SimpleInOutDialog("A new customer");
//input
S = "Name customer: " + input.readString("Give in your name:");
WriteToFile();
S = "Adress: " + input.readString("Give your adress");
WriteToFile();
S = "Telephonenummber: " + input.readString("Give your telephonenumber");
WriteToFile();
//making a customerID
UUID idCustomer = UUID.randomUUID();
S = "CustomerID: " + customerID.toString();
WriteToFile();
}
public void WriteToFile(){
try{
FileWriter writer = new FileWriter("L:\\Documents/Informatica/6de jaar/GIP/Customer.txt", true);
BufferedWriter out = new BufferedWriter(writer);
//Wrting away your data
out.write(S + NEWLINE);
//Closing the writer
out.close();
}catch (Exception e){//Catch when there are errors
System.err.println("Error: " + e.getMessage());
}
}
}
A BufferedReader() has a method named readLine(), which you can use to read a line from a file:
BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;
while ((line = br.readLine()) != null)
{
...
}
br.close();
From your WriteToFile() method it seems a customer's details occupies four lines, with the name of the customer appearing on the first line. When searching for a customer, arrange the while loop to only examine every fourth line read.
Other points:
There appears to be no reason for S to be a member variable, nevermind a static. Just declare a local String instance in setNewCustomer() and pass it as an argument to WriteToFile().
Instead of defining a NEWLINE variable you can use BufferedWriter's newLine() method.

Categories