FileWriter not writing all of its contents - how to get around maximum? - java

After a good amount of time researching the issue, and doing a little counting/debugging on my own, I found out that the max number of characters a FileWriter can contain at once is about 1216. Can anyone help me figure out how to get around this issue?
I'm essentially writing a script in Java to combine two .csv files because I need to put the contents of each file side by side in one .csv file. I'm iterating through and appending the characters, along with commas and new lines, to the file writer. However, all of my outputs are stopping about 80% through the writing process. I'm not getting any errors, but it appears obvious to me now that it is an issue of FileWriter capacity.
If I write what I have then start appending to a new FileWriter, I will lose my spot in the .csv file. I'm not really sure what to do here.
I look forward to feedback. Thank you!!
EDIT - code
The problem is with the mergedWriter.
I've verified that the length of "mainLines[j].size() - 1" is 101, and it is only writing 85ish lines for my outputs. It stops mid-string.
for (int k = 0; k < mainLines[j].size() - 1; k++) {
// All deletions for bottleneck
if (k != 0) {
eachEffWriter.append(mergedFileNum + COMMA + "1," + mainLines[j].get(k)[6] + NEWLINE);
efficiencyWriter.append(mergedFileNum + COMMA + "1," + mainLines[j].get(k)[5] + NEWLINE);
}
// Write deletionoutput data
StringBuilder builder = new StringBuilder();
for (String str : mainLines[j].get(k)) {
builder.append(str);
builder.append(COMMA);
}
builder.append(COMMA);
if (k == 0) {
builder.append(COMMA);
}
for (String str : kingpinLines[j].get(k)) {
builder.append(str);
builder.append(COMMA);
}
mergedWriter.append(builder.toString());
mergedWriter.append(NEWLINE);
}

Solved.
I wasn't flushing/closing my writer before re-instantiating it to a new file. I should have either made an array of mergedWriters, or flushed/closed it each time before re-assigning it.

Related

Having problems scanning a String from a file in Java

I'm trying to scan a line of text from a .txt file, split it up into seven numbers, change two of the numbers, and then write the new numbers back into the .txt file. The code below works fine the first time, but seems to have issues with reading from the text file a second time for the new starting String. I've done very similar things multiple times and had no issues, so I'm really not sure why I'm having problems this time around. The code I currently have is:
public void addWin(int numGuesses) throws IOException {
FileWriter writer = new FileWriter(*filepath*);
Scanner scan = new Scanner(new File(*filepath*));
String temp = "0;0;0;0;0;0;0;";
if (scan.hasNextLine()) {
temp = scan.nextLine();
}
String[] statsArr = temp.split(";");
scan.close();
statsArr[0] = Integer.toString(Integer.parseInt(statsArr[0]) + 1);
statsArr[numGuesses] = Integer.toString(Integer.parseInt(statsArr[numGuesses]) + 1);
for (int i = 0; i < statsArr.length; i++) {
writer.append(statsArr[i] + ";");
}
writer.close();
}
Some extra context if needed, this is essentially for a Wordle clone sort of thing for a Discord bot I have. numGuesses is the number of guesses it took to get the word correct. The String being written in and being read is 7 numbers divided up by a semicolon, the first number is the current win streak, the second number is number of times you've won in 1 guess, and so on. The testing I've done seems to place the error somewhere before the scanner closes. A first run through will correctly write the numbers, so if the word was guessed in 3 attempts the file will contain "1;0;0;1;0;0;0;", but the next time the method is called it essentially starts from scratch. Checking the temp variable just after the if statement on a second run through just shows "0;0;0;0;0;0;0;". Sorry for the long-windedness, just trying to provide all possibly helpful details. Thank you in advance!
-
Consider the JavaDoc which states "Whether or not a file is available or may be created depends upon the underlying platform.". So what is happening here, is that when you use new FileWriter(*filepath*) the file is being locked/created blank, so when you use new Scanner(new File(*filepath*)); and scan.hasNextLine() you get a null/empty value.
The easy solution is to simply move the FileWriter further down in your code, and only open it after the scanner has been closed. Also add an else to your if statement so you know if there is an issue with reading from the scanner:
//Move the below line to be later in the code
//FileWriter writer = new FileWriter(*filepath*);
Scanner scan = new Scanner(new File(*filepath*));
String temp = "0;0;0;0;0;0;0;";
if (scan.hasNextLine()) {
temp = scan.nextLine();
}
//Add some debugging
else{
System.out.println("ERROR no data could be read");
}
String[] statsArr = temp.split(";");
scan.close();
statsArr[0] = Integer.toString(Integer.parseInt(statsArr[0]) + 1);
statsArr[numGuesses] = Integer.toString(Integer.parseInt(statsArr[numGuesses]) + 1);
//Create the flie writer here instead
FileWriter writer = new FileWriter(*filepath*);
for (int i = 0; i < statsArr.length; i++) {
writer.append(statsArr[i] + ";");
}
writer.close();
Now assuming the file exists and can be edited, and where numGuesses = 3, then for the following contents:
1;2;3;4;5;6;7;
The output of running the code is as expected (+1 to the 0 and 3rd index)
2;2;3;5;5;6;7;
The reason you only saw 0;0;0;0;0;0;0; was because the code was failing to read from the scanner, and always using the temp value from this line String temp = "0;0;0;0;0;0;0;";. By adding the else check above we can see when it fails.
FileWriter writer = new FileWriter(*filepath*); clears the contents of the file you are trying to read from. You need to move this line after scan.close();

Java - Best way to open a ton a files and search for a word?

I am searching a directory with about 450 files, each file around 20kb. Here is my method:
public void search(String searchWord) throws IOException
{
this.directoryPath = FileSystems.getDefault().getPath(this.directoryString);
this.fileListStream = Files.newDirectoryStream(this.directoryPath);
int fileCount = 0;
for(Path path : this.fileListStream)
{
String fileName = path.getFileName().toString();
if(!fileName.startsWith("."))
{
BufferedReader br = Files.newBufferedReader(path, Charset.defaultCharset());
String line;
while((line = br.readLine()) != null)
{
System.out.println(fileName + ": " + line);
}
fileCount++;
br.close();
}
}
System.out.println("File Count: " + fileCount);
}
My goal is to go word by word and find a match for searchWord and print out the line number and the file name it was found in.
My problem is that I'm wondering if I should split the line into an array and search the array for the word and add it to a list. Or should I scan the entire file into an array of words and then search for the words and add them to a list? Or does it even matter? Also, if there is a better way to do this, please let me know! I'm trying to do this as efficient as possible due to limited resources.
You shouldn't be looking word-by-word, just read the entire line as a String and then use String.indexOf() method to find if the line contains the word or not.
You can use Scanner class to parse files and use its next() method to read each word so you won't require any array or other storage. Try to use multi-threading if possible for each file which can even improve performance.

Reading large numbers of text files in java

I have an application that needs to read only specific content from a text file. I have to read the text from 10,000 different text files arranged in a folder and have to populate the content from all those text files into a single CSV file.
My application runs fine, but it is reading up to file number 999 only. No error, but is not reading file after 999.
Any ideas?
public void calculate(String location) throws IOException{
String mylocation = location;
File rep = new File(mylocation);
File f2 = new File (mylocation + "\\" + "metricvalue.csv");
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter (fw);
if(rep.exists() && rep.isDirectory()){
File name[] = rep.listFiles();
for(int j = 0; j < name.length; j++){
if(name[j].isFile()){
String filename = name[j].getPath();
String nameinfo = name[j].getName();
File f1= new File (filename);
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader (fr);
String line = null;
while((line = br.readLine()) != null){
if(line.contains(" | #1 #2 % Correct")){
bw.write(nameinfo + ",");
while((line=br.readLine()) != null) {
if((line.indexOf("#" ) != -1)){
String info[] = line.split("\\s+");
String str = info[2] + "," + info[3] + ",";
bw.write(str);
}
}
}
}
bw.newLine();
br.close();
}
}
}
bw.close();
}
Your platform's file system is limited to 999 open files. You may need to increase the limit or close() the FileReader explicitly:
fr.close();
How to debug:
Put a breakpoint at File name[] = rep.listFiles();
Open variables when Eclipse pauses and check that your array contains all of the file names you want. This will tell you if your problem is there or in your parsing.
You need to debug your code. Here are a couple of pointers to get you started:
File name[] = rep.listFiles();
for(int j =0;j<name.length; j++) {
if(name[j].isFile()) {
What is the size of the array? Figure it out. If there are 10000 elements in the array, that's how many iterations your loop will do, there is simply no other way. Just adding
System.out.println(name.length) will answer this question for you
If the array is shorter than 10000, that's your answer, you simply counted your files incorrectly. If it is not, then your problem must be that one of the "files" isn't really a file (and the test of the if statement fails). Add an else statement to it, and print out the name ... Or better yet, remove this if at all (in general, avoid nested conditionals encompassing the entire body of an outer structure, especially, huge ones like this, it makes your code fragile, and logic very hard to follow), and replace it with
if(!name[j].isFile()) {
System.out.println("Skipping " + name[j] + " because it is not a plain file.");
continue;
}
This will tell you which of 10000 files you are skipping. If it does not print anything, that means, that you do in fact read all 10000 files, as you expect, and the actual problem causing the symptom you are investigating, is elsewhere.

Eliminating unnecessary space on a String array using Java?

I have a String array in Java (Using the NetBeans IDE) containing a small text. When inserting the lines of text into the array I ended up with too much unnecessary space and characters which I would like to get rid of. Here is an example:
1 experimental investigation of the aerodynamics of a wing in a slipstream . an experimental study of a wing [...] flow theory . an empirical [...] .
2 small viscosity . in the study of high-speed [...] vorticity . the discussion here is restricted to two-dimensional incompressible steady flow .
As you can see, in some cases I end up with 3 spaces between a period and the next word. How do I get rid of the extra space and characters such as periods, commas, etc?
Edit: Here is the process.
-Inserting the text on x position within the String array:
try{
coleccion = new File (File location);
fr = new FileReader (coleccion);
br = new BufferedReader(fr);
String numDoc = " ";
int pos = 0;
while((numDoc=br.readLine())!=null){
if(numDoc.contains(".W")){
while((numDoc=br.readLine())!= null && !numDoc.contains(".I")){
if(Text[pos] != null) {
Text[pos] = Texto[pos] + " " + numDoc;s
}
else {
Text[pos] = numDoc;
}
}
pos++;
}
}
}
catch(Exception e){
e.printStackTrace();
}
-Printing the array (1400 positions):
for(int i=0; i<=1399; i++){
//System.out.println(ID[i]);
System.out.println(i+1 + " " + Texto[i]);
}
-Extra info on the initial problem:
.txt file to arrays using Java
So i guess that by unnecessary space you mean consecutive spaces, if that's the case then this is what you want : https://stackoverflow.com/a/2932439/4088809
replaceAll("^ +| +$|( )+", "$1")
that's what you're looking for, just apply it to the entire line you get with the readLine, that should do the trick.

How can I write to a specific line number in a txt file in Java

I'm currently writing my project for school in which requires me to read and write to txt files. I can read them correctly but I can only write to them at the end from an appended FileWriter. I would like to be able to overwrite things in my txt files on line numbers by first deleting the data on the line and then writing in the new data. I attempted to use this method...
public void overWriteFile(String dataType, String newData) throws IOException
{
ReadFile file = new ReadFile(path);
RandomAccessFile ra = new RandomAccessFile(path, "rw");
int line = file.lineNumber(path, dataType);
ra.seek(line);
ra.writeUTF(dataType.toUpperCase() + ":" + newData);
}
but I believe that the seek method moves along in bytes rather than line numbers. Can anyone help. Thanks in advance :)
P.S. the file.lineNumber method returns the exact line that the old data was on so I already have the line number that needs to be written to.
EDIT: Soloution found! Thanks guys :) I'll post the soloution below if anyone is interested
public void overWriteFile(String dataType, String newData, Team team, int dataOrder) throws IOException
{
try
{
ReadFile fileRead = new ReadFile(path);
String data = "";
if(path == "res/metadata.txt")
{
data = fileRead.getMetaData(dataType);
}
else if(path == "res/squads.txt")
{
data = fileRead.getSquadData(dataType, dataOrder);
}
else if(path == "res/users.txt")
{
data = fileRead.getUsernameData(dataType, dataOrder);
}
else if(path == ("res/playerdata/" + team.teamname + ".txt"))
{
//data = fileRead.getPlayerData(dataType, team.teamname, dataOrder);
}
BufferedReader file = new BufferedReader(new FileReader(path));
String line;
String input = "";
while((line = file.readLine()) != null)
{
input += line + '\n';
}
input = input.replace(dataType.toUpperCase() + ":" + data, dataType.toUpperCase() + ":" + newData);
FileOutputStream out = new FileOutputStream(path);
out.write(input.getBytes());
}
catch(Exception e)
{
System.out.println("Error overwriting file: " + path);
e.printStackTrace();
}
}
A quick and dirty solution would be to use the Files.readAllLines and Files.write methods to read all lines, change the one you want to change, and overwrite the whole file:
List<String> lines = Files.readAllLines(file.toPath());
lines.set(line, dataType.toUpperCase() + ":" + newData);
Files.write(file.toPath(), lines); // You can add a charset and other options too
Of course, that's not a good idea if it's a very big file. See this answer for some ideas on how to copy the file line by line in that case.
Regardless of how you do it, though, if you are changing the byte length of the line, you will need to rewrite the whole file (AFAIK). RandomAcessFile allows you to move around the file and overwrite data, but not to insert new bytes or removes existing ones, so the length of the file (in bytes) will stay the same.
Here is a link to a question just like this with a great answer:
I want to open a text file and edit a specific line in java
Basically, you can't just edit that line, unless it'll be the exact same length.
Instead, you'll want to copy over every line, and then when you reach the line number of the line you want to change, instead of copying over the old line, just put in your new line.
The link I gave you has a great example on how to do this.
I hope this helps...if not, let me know, and I'll elaborate further on the post. Good luck :)

Categories

Resources