Name file outputStream with a running Number - java

I am Splitting large files along a Line END OF STATEMENT and writing new files. I need to name the files with a running number i.e Statement1, Statement2.... Here is what I have:
String[] filenames1 = statements.list(only);//only is filenamefilter
int count;
for (int k = 0; k < filenames1.length; k++) {
try {
FileInputStream fs = new FileInputStream("C:/statements/" + filenames1[k]);
System.out.println(filenames1[k]);
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
count = 0;
FileOutputStream fos = new FileOutputStream("C:/ABC Statements/Statement" + count + ".RPT");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
while ((lines = br.readLine()) != null) {
String mine = lines.trim();
if (mine.startsWith("********END OF STATEMENT********")) {
bw.close();
fos.close();
count++;
fos = new FileOutputStream("C:/ABC Statements/Statement" + count + ".RPT");
bw = new BufferedWriter(new OutputStreamWriter(fos));
continue;
}
if (mine.isEmpty()) {
continue;
}
bw.write(lines);
bw.newLine();
bw.flush();
}
fs.close();
br.close();
fos.close();
bw.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
I am getting only one file with Statement0 Meaning the names are getting overwritten. What Exactly am I doing wrong with count++

You should append an identifier to your output files, to know to what source file they are related, and prevent overwriting.
For instance :
String srcPrefix = filenames1[k].substring(0, filenames1[k].lastIndexOf('.'));
String destFilePath = "C:/ABC Statements/Statement" + "_" + srcPrefix + "_" + count + ".RPT";

I actually got it. Initializing count outside the for loop somehow solved it.
String[] filenames1 = statements.list(only);//only is filenamefilter
int count = 0;
for (int k = 0; k < filenames1.length; k++) {
try {
FileInputStream fs = new FileInputStream("C:/statements/" + filenames1[k]);
System.out.println(filenames1[k]);
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
FileOutputStream fos = new FileOutputStream("C:/ABC Statements/Statement" + count + ".RPT");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
while ((lines = br.readLine()) != null) {
String mine = lines.trim();
if (mine.startsWith("********END OF STATEMENT********")) {
bw.close();
fos.close();
count++;
fos = new FileOutputStream("C:/ABC Statements/Statement" + count + ".RPT");
bw = new BufferedWriter(new OutputStreamWriter(fos));
continue;
}
if (mine.isEmpty()) {
continue;
}
bw.write(lines);
bw.newLine();
bw.flush();
}
fs.close();
br.close();
fos.close();
bw.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}

Related

Java, encoding with my custom cipher

I have a program that encode and decodes with my custom cipher, text files and lossless media files, but the problem is that over 2MB it crashes.
void doTheRabi(File f, byte[] hashedPass) {
try {
// BufferedReader br = new BufferedReader(new InputStreamReader(new
// FileInputStream(f))); // legge il file
// String response = new
// String(Files.readAllBytes(Paths.get(f.getAbsolutePath()))); // scrive tutto
// il file in memoria
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String response = new String(); // ASSEGNO IL CONTENUTO DEL FILE IN QUESTA STRINGA
for (String line; (line = br.readLine()) != null; response += line + "\n")
;
response = response.replace("\n", "newline").replace("\r", "newrow"); // rimpiazzo le new line con "newline"
// e "newrow"
byte[] encodedfile = response.getBytes(StandardCharsets.UTF_8); // trasformo il file in byte
byte[] result = new byte[encodedfile.length]; // variabile temporanea
int hpc = 0;
for (int i = 0; i < result.length; i++) {
result[i] = (byte) (encodedfile[i] + hashedPass[hpc++]); // algoritmo rabi
if (hpc == hashedPass.length) {
hpc = 0;
}
}
String encodedresult = Base64.getEncoder().encodeToString(result); // restituisco il risultato in base64
FileWriter fw = new FileWriter(f);
PrintWriter pw = new PrintWriter(fw);
pw.print("");
pw.append(encodedresult /* + "extension=" + extString */); // scrivo nel file tutto il risultato
pw.flush();
pw.close();
fw.close();
br.close();
String path = f.getAbsolutePath();
String newName = path + ".rab1";
f.renameTo(new File(newName));
} catch (Exception e) {
console.appendText("Error: " + e.getMessage() + "\n");
e.printStackTrace();
}
}
// operazione inversa
void killTheRabi(File f, byte[] hashedPass) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String response = new String();
for (String line; (line = br.readLine()) != null; response += line)
;
byte[] decodedfile = Base64.getDecoder().decode(response);
byte[] result = new byte[decodedfile.length];
int hpc = 0;
for (int i = 0; i < result.length; i++) {
result[i] = (byte) (decodedfile[i] - hashedPass[hpc++]);
if (hpc == hashedPass.length) {
hpc = 0;
}
}
String resultString = bytesToString(result);
String finalres = resultString.replace("newline", "\n").replace("newrow", "\r");
FileWriter fw = new FileWriter(f);
PrintWriter pw = new PrintWriter(fw);
pw.print("");
pw.append(finalres);
pw.flush();
pw.close();
fw.close();
br.close();
String path = f.getAbsolutePath();
String newName = path.replace(".rab1", "");
f.renameTo(new File(newName));
} catch (Exception e) {
console.appendText("Error: " + e.getMessage() + "\n");
e.printStackTrace();
}
}
What am I doing wrong? I think it's because the memory gets full, since java uses a virtual machine, but I don't know a way to enhance the memory usage, maybe using buffers but am I not using them already?
Since you are possibly holding quite a bit of data in memory, try the following:
Increase maximum heap size to be used by the JVM by starting with the parameter -Xmx2048m or more

Change a value in a file Java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So, I want to change a value in the file Utilizadores.txt
the default value is zero but when i click in the button the value has to change to 10 and every time i do a new one the value gets +10
the problem is when e try to do that the space where it should appear the (10/20/30...) appears blank
PS: it only changes in the lines of the file that have a certain ID(the id of my user)
private void ConfirmarActionPerformed(java.awt.event.ActionEvent evt) {
String filePath = "Reservas.txt";
File file = new File(filePath);
try {
FileWriter fw1 = new FileWriter(file, true);
BufferedWriter bw1 = new BufferedWriter(fw1);
bw1.write(this.id+ "-" + cbxrestaurante.getSelectedItem().toString() + "-" + Dia.getText() + "-" + Hora.getText() + "-" + Lugares.getText());
bw1.newLine();
bw1.close();
fw1.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(ClienteForm.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ClienteForm.class.getName()).log(Level.SEVERE, null, ex);
}
String filePath1 = "Utilizadores.txt";
File file1 = new File(filePath1);
try {
FileReader fr = new FileReader(file1);
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(file1, true);
PrintWriter bw = new PrintWriter(fw);
/*BufferedReader br = new BufferedReader(new FileReader(file1));
FileWriter fw = new FileWriter(file1, true);
PrintWriter bw = new PrintWriter((file1), "UTF-8");*/
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file1, true)));
Object[] lines = br.lines().toArray();
Integer[] getPontos = new Integer[lines.length];
String[] identificador = new String[lines.length];
String[] cartao = new String[lines.length];
String[] Pass = new String[lines.length];
String[] Nome = new String[lines.length];
String[] NIF = new String[lines.length];
String[] Tele = new String[lines.length];
String Zero = "zero";
String[] Morada = new String[lines.length];
String[] Localidade = new String[lines.length];
String[] dono = new String[lines.length];
for (int i = 0; i < lines.length; i++) {
String[] row = lines[i].toString().split("-");
if (Objects.equals(Zero, row[10])||Integer.parseInt(row[10])==0) {
getPontos[i] = 0;
} else {
getPontos[i] = Integer.parseInt(row[10]);
}
identificador[i] = row[1];
cartao[i] = row[2];
Pass[i] = row[3];
Nome[i] = row[4];
NIF[i] = row[5];
Tele[i] = row[6];
Morada[i] = row[7];
Localidade[i] = row[8];
dono[i] = row[9];
if (Integer.parseInt(identificador[i]) == this.id) {
if (getPontos[i] == 0) {
getPontos[i] = 10;
} else {
getPontos[i] = getPontos[i] + 10;
}
}
}
fr.close();
br.close();
File temp = new File("Utilizadores.txt");
if (temp.exists()) {
RandomAccessFile raf = new RandomAccessFile(temp, "rw");
raf.setLength(0);
}
for (int i = 0; i < lines.length; i++) {
bw.write("UserId-");
bw.write(identificador[i] + "-");
bw.write(cartao[i] + "-");
bw.write(Pass[i] + "-");
bw.write(Nome[i] + "-");
bw.write(NIF[i] + "-");
bw.write(Tele[i] + "-");
bw.write(Morada[i] + "-");
bw.write(Localidade[i] + "-");
bw.write(dono[i] + "-");
bw.write(getPontos[i]);
bw.write("\r\n");
}
bw.close();
fw.close();
this.dispose();
} catch (FileNotFoundException ex) {
System.out.println("Import nao funciona");
} catch (IOException ex) {
Logger.getLogger(ClienteForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
Try to convert your Integer to String:
bw.write( String.valueOf(getPontos[i]));
instead
bw.write(getPontos[i]);

Read a particular info and write to a txt file using java

am new to java programming I need a program to read a certain information from a file and select the particular informationwhich is needed and then write this particular information into a text file .
{
BufferedReader in = new BufferedReader (newFileReader("C:/Users/ngorentl/"));
String info = "";
String info1 = "";
int startLine = 111 ;
int endLine = 203 ;
int sl = 221;
int el =325;
// reading only the specific info which is needed and that is printing in the console
for (int i = 0; i < startLine; i++) { info = in.readLine(); }
for (int i = startLine; i < endLine + 1; i++) {
info = in.readLine();
System.out.println(info);
}
for (int j = 203; j < sl; j++) { info1 = in.readLine(); }
for (int j = sl; j < el + 1; j++) {
info1 = in.readLine();
System.out.println(info1);
}
// having a problem from here i dont know whether this is the correct approach
File fin = new File(info); // getting an error here
FileInputStream fis = new FileInputStream(fin);
BufferedReader is = new BufferedReader(new InputStreamReader(fis));
FileOutputStream fos = new FileOutputStream("hh.txt");
OutputStreamWriter osw= new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
String aLine = null;
while ((aLine = is.readLine()) != null) {
bw.write(aLine);
bw.newLine();
bw.flush();
bw.close();
is.close();
}
in.close();
}
File fin = new File(String fileName) is the correct syntax.
eg.
File fin = new File("C:\abc.txt");
[UPDATE]
Assuming your question is about writing a String to file.
In Java 7
try( PrintWriter out = new PrintWriter( "filename.txt" ) ){
out.println( info);
}
In Java 6 or below, use
try {
BufferedWriter out = new BufferedWriter(new FileWriter("sample.txt"));
out.write(info);
out.close();
}
catch (IOException e)
{
System.out.println("Exception ");
}
and possible correction of your code, so variable info has all that information
for (int i = startLine; i < endLine + 1; i++) {
info += in.readLine();
}
System.out.println(info);

Can't write all list to the file

I have an ArrayList list of some lines from text file. I am trying to find these lines in a text file, if I find it I want to write it to another text file and delete it from the original file.
I wrote a code for that, it is working but not for the whole list, sometimes take one line and sometimes take more. and give me this message:
1 R101 100850 0
Exception caught : java.io.IOException: Stream closed
static void moveLines(ArrayList posList, int topic) {
//=======================To read lines=======
File inputFile = new File("U:\\Research\\Projects\\sef\\enhancfeaturtm\\TestData\\topic\\" + "Test" + topic + ".txt");
File outputFile = new File("U:\\Research\\Projects\\sef\\enhancfeaturtm\\TestData\\topic\\" + "Training" + topic + ".txt");
try {
FileReader fr = new FileReader(inputFile);
BufferedReader br = new BufferedReader(fr);
FileWriter fr1 = new FileWriter(outputFile);
BufferedWriter writer = new BufferedWriter(fr1);
String line;
int count = 1;
int z = 1;
while ((line = br.readLine()) != null) {
// System.out.println(z++ + ": ");
String subLine = line.substring(5, line.length() - 2);
// System.out.println(subLine);
if (posList.contains(subLine)) {
System.out.println(count++ + " " + line);
fr1.write(line);
fr1.write("\n");
fr1.flush();
fr.close();
removeLineFromFile(inputFile.getAbsolutePath(), line);
}
}
br.close();
fr1.close();
writer.close();
} catch (Exception e) {
System.out.println("Exception caught : " + e);
}
}
static void removeLineFromFile(String file, String lineToRemove) {
try {
File inFile = new File(file);
//Construct the new file that will later be renamed to the original filename.
File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;
//Read from the original file and write to the new
//unless content matches data to be removed.
while ((line = br.readLine()) != null) {
if (!line.trim().equals(lineToRemove)) {
pw.println(line);
pw.flush();
}
}
pw.close();
br.close();
//Delete the original file
if (!inFile.delete()) {
System.out.println("Could not delete file");
return;
}
//Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile)) {
System.out.println("Could not rename file");
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
Can someone help me please?
String subLine = line.substring(5, line.length() - 2);
You are hard coding to take substring from index 5.
What happens when the length of line is less than 5? have a check if the line's length is less than 5 and only then proceed.
Also why are catching with 'Exception' ? try catching with a lower level exception like ArrayIndexOutOfBoundsException etc.
Thank you all for your help.
I figure out the problem, it was because I delete the file and create it again from temp file. in that case I lose the pointer to the file.
This is the code after I fix it if someone interested.
static void moveLines(ArrayList posList, int topic) {
//=======================To read lines=======
File inputFile = new File("U:\\Research\\Projects\\sef\\enhancfeaturtm\\Data1\\topic\\" + "Test" + topic + ".txt");
File outputFile = new File("U:\\Research\\Projects\\sef\\enhancfeaturtm\\Data1\\topic\\" + "Training" + topic + ".txt");
try {
FileReader fileReader = new FileReader(inputFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
FileWriter fileWriter = new FileWriter(outputFile);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
String line;
while ((line = bufferedReader.readLine()) != null) {
String subLine = line.substring(5, line.length() - 2);
if (posList.contains(subLine)) {
System.out.println(count++ + " " + line);
bufferedReader.close();
fileReader.close();
bufferedWriter.write(line+"\n");
bufferedWriter.flush();
bufferedReader = removeLineFromFile(inputFile.getAbsolutePath(), line);
}
}
bufferedWriter.close();
fileWriter.close();
bufferedReader.close();
fileReader.close();
} catch (Exception e) {
System.out.println("Exception caught : " + e);
}
}
static BufferedReader removeLineFromFile(String file, String lineToRemove) {
BufferedReader bufferedReader = null;
try {
File inFile = new File(file);
//Construct the new file that will later be renamed to the original filename.
File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(file));
BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));
String line = null;
//Read from the original file and write to the new
//unless content matches data to be removed.
while ((line = br.readLine()) != null) {
if (!line.trim().equals(lineToRemove)) {
bw.write(line+"\n");
bw.flush();
}
}
bw.close();
br.close();
//Delete the original file
if (!inFile.delete()) {
System.out.println("Could not delete file");
return null;
}
//Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile)) {
System.out.println("Could not rename file");
}
FileReader fileReader = new FileReader(inFile);
bufferedReader = new BufferedReader(fileReader);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return bufferedReader;
}

How to write in existing file from list using Java by removing duplicate data?

I want to write in file that exists.
My data is in the form of java list.
Here is a sample of data :
snmp,192.168.20.1,cloud,
snmp,192.168.20.2,cloud,
I want to add line snmp,192.168.20.1,cloud123 in the file.
It should update existing file content i.e.(snmp,192.168.20.1,cloud) by new contents given.
And if provided contents different from contents of file then append it to file?
Here is my workaround---
String tempFile = RunMTNew.instdir + "/var/";
File tempFileName = new File(tempFile+"hosts.tempFile");
try{
if(!tempFileName.exists()) {
tempFileName.createNewFile();
}
}catch (FileNotFoundException e){
e.printStackTrace();
}
catch ( IOException ioe){
ioe.printStackTrace();
System.out.println("Exception occured while creating temp file");
}
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
PrintWriter tempoutfile= null;
while((line = bufferedReader.readLine()) != null) {
System.out.println("line before if "+ line);
if(line!=null || (line = line.trim()) != "" ){
System.out.println("line at start of while" + line);
String[] lineFromFile = line.split(",");
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filename, true)));
ListIterator atwlist = arraytowrite.listIterator();
String lineToWriteInFile = "";
while (atwlist.hasNext()) {
ArrayList atwlistline = (ArrayList) atwlist.next();
System.out.println("array" + atwlistline);
String lineToAdd = atwlistline.toString();
lineToWriteIfNotFound = lineToAdd;
System.out.println("After converting to string line is" + lineToAdd);
System.out.println("lineFromFile contents are "+ lineFromFile[1]);
if(lineToAdd.contains(lineFromFile[1])){
lineToWriteInFile = lineToAdd;
}
else{
lineToWriteInFile = line;
}
}
try{
tempoutfile = new PrintWriter(new BufferedWriter(new FileWriter(tempFileName, true)));
System.out.println("writing in file" +lineToWriteInFile);
tempoutfile.write(lineToWriteInFile);
tempoutfile.write("\n");
}catch(IOException ioe){
ioe.printStackTrace();
System.out.println("Exception occured while writing in tempFile");
}
tempoutfile.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Exception occured in outer try block");
}
}//end of if
}// end of while
try{
FileReader tempfileReader = new FileReader(tempFile+"hosts.tempFile");
BufferedReader tempBufferedReader = new BufferedReader(tempfileReader);
FileWriter fosFinal = new FileWriter(filename);
PrintWriter outFinal = new PrintWriter(fosFinal);
while((line = tempBufferedReader.readLine()) != null) {
System.out.println("line from tempfile to write in main " + line);
outFinal.write(line);
}
}catch(IOException ie){
ie.printStackTrace();
System.out.println("Exception occured while reading from temp and write into main file");
}
Use temp file to store temporarily contents of file.
Here is a code :
ListIterator atwlist = arraytowrite.listIterator();
while (atwlist.hasNext()) {
ArrayList atwlistline = (ArrayList) atwlist.next();
ListIterator atwlistlineL = atwlistline.listIterator();
while (atwlistlineL.hasNext()) {
firstWriter.write((String) atwlistlineL.next());
firstWriter.write(",");
}
//firstWriter.write("\n");
System.out.println(atwlistline);
}
firstWriter.close();
//Write original file contents to another file i.e. tempFile2
try{
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
String line = "";
while((line = bufferedReader.readLine()) != null) {
secondWriter.write(line);
secondWriter.write("\n");
}
secondWriter.close();
bufferedReader.close();
}catch(IOException ie){
ie.printStackTrace();
System.out.println("Exception occured while reading from main file");
}
//check and remove duplicate entries from file
try{
FileReader singleDeviceReader = new FileReader(file1Path);
FileReader duplicateDeviceReader = new FileReader(file2Path);
finalWriter = new PrintWriter(filename);
BufferedReader bufferedReader1 = new BufferedReader(singleDeviceReader);
BufferedReader bufferedReader2 = new BufferedReader(duplicateDeviceReader);
String line1 = null;
String line2 = null;
boolean fileWriteFlag = false;
String ifNotFind = "";
while((line1 = bufferedReader1.readLine())!=null){
String[] line1Split = line1.split(",");
ifNotFind = line1;
while((line2 = bufferedReader2.readLine())!=null){
String[] line2Split = line2.split(",");
if (line2Split[1].equals(line1Split[1])){
finalWriter.write(line1);
finalWriter.write("\n");
fileWriteFlag = true;
}
else {
finalWriter.write(line2);
finalWriter.write("\n");
}
}
}
if(!fileWriteFlag){
finalWriter.write(ifNotFind);
}
finalWriter.close();
bufferedReader1.close();
bufferedReader2.close();
File t1 = new File (file1Path);
File t2 = new File (file1Path);
if (t1.exists()){
t1.delete();
}
if (t2.exists()){
t2.delete();
}
}catch (IOException ioe ){
ioe.printStackTrace();
System.out.println("Exception occured while reading both temp files");
}
Instead of checking and comparing the content from the file I suggest you create list with unique items. Which will save your time to parse the file content and update operations on file and your code as well
First get all the existing data set to a list. now iterate your new list(which need to append) using a for loop and get a inner loop and check condition and add/skip.
Pseudo code:
List my_existing_list;
List my_new_list;
foreach(my_new_list){
foreach(my_existing_list){
if(equal to an existing item){
//skip
}else{
//append to file
}
}
}

Categories

Resources