Reputation: 98
The objective of the code is to read a file character by character, converting each to lowercase and replacing non-alphabets with a space. There are 500 lines in the original file but the edited file ends abruptly.
File fileToBeModified = new File(filePath);
BufferedReader reader = null;
FileWriter writer = null;
try
{
reader = new BufferedReader(new FileReader(fileToBeModified));
char single;
String lowercase="";
int a = reader.read();
writer = new FileWriter(fileToBeModified);
while(a!=-1){
single = (char) a;
String s = String.valueOf(single);
if (a>=65 && a<=90 || a>=97 && a<=122)
lowercase = s.replace(s, s.toLowerCase());
else if(s.equals("0")==false && s.equals("1")==false && s.equals("'")==false)
lowercase = s.replace(s, " ");
writer.write(lowercase);
a = reader.read();
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
//Closing the resources
reader.close();
writer.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
How can I get the program to perform the actions on the entire file?
Here is a part of the file. It is a dataset 500 lines of restaurant reviews and the rest of the file is in the same format.
Wow... Loved this place. 1
Crust is not good. 0
Not tasty and the texture was just nasty. 0
Stopped by during the late May bank holiday off Rick Steve recommendation and loved it. 1
The selection on the menu was great and so were the prices. 1
Now I am getting angry and I want my damn pho. 0
Honeslty it didn't taste THAT fresh.) 0
The potatoes were like rubber and you could tell they had been made up ahead of time being kept under a warmer. 0
The fries were great too. 1
A great touch. 1
Service was very prompt. 1
Would not go back. 0
The cashier had no care what so ever on what I had to say it still ended up being wayyy overpriced. 0
I tried the Cape Cod ravoli, chicken,with cranberry...mmmm! 1
I was disgusted because I was pretty sure that was human hair. 0
I was shocked because no signs indicate cash only. 0
Highly recommended. 1
Waitress was a little slow in service. 0
This place is not worth your time, let alone Vegas. 0
did not like at all. 0
The Burrittos Blah! 0
The food, amazing. 1
Upvotes: 0
Views: 133
Reputation: 2694
I have tried to simplify the solution leveraging Java8
, stream
and regular expression
.
readAllLines
: This method read all lines from a file. Bytes from the file are decoded into characters using the UTF-8 charset. Refer to the doc : class-files
for details.
Note : You can further change regular expressions as per your requirement / use-case.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class FileRead
{
public static void main(String[] args) throws IOException {
generateArrayListFromFile("lines.txt");
}
public static ArrayList<String> generateArrayListFromFile(String filename) throws IOException {
List<String> result = Files.readAllLines(Paths.get(filename));
result.stream().map(String::toLowerCase).collect(Collectors.toList());
List<String> withoutNumbers = new ArrayList<>();
for (String s : result) {
withoutNumbers.add(s.replaceAll("\\d", ""));
}
System.out.println(withoutNumbers);
return (ArrayList<String>) withoutNumbers;
}
}
Upvotes: 1
Reputation: 1422
A file may contain different types of characters. To read this character, sometimes BufferedReader.reader() may throw an exception. I tried to execute your code using your input format. But that time without any issue it executed. So my suggestion is to use an additional try-catch block when to read and to write to file. Moreover, String operation is very costly and not memory efficient. I modify your code and share it here, hope it will be helpful to you.
String filePath = "hello.txt";
File fileToBeModified = new File(filePath);
System.out.println(fileToBeModified.exists());
BufferedReader reader = null;
FileWriter writer = null;
try {
reader = new BufferedReader(new FileReader(fileToBeModified));
char single;
//String lowercase = "";
char lowercase = ' ';
int a = reader.read();
writer = new FileWriter(fileToBeModified);
while (a != -1) {
single = (char) a;
//String s = String.valueOf(single);
if (a >= 65 && a <= 90 || a >= 97 && a <= 122)
lowercase = Character.toLowerCase(single);//s.replace(s, s.toLowerCase());
//else if (s.equals("0") == false && s.equals("1") == false && s.equals("'") == false)
else if(single != '0'&& single != '1' && single != '\'' )
lowercase = ' ';
//additional try-catch block
try {
writer.write(lowercase);
a = reader.read();
}catch(Exception ex) {
ex.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// Closing the resources
reader.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Upvotes: 1