I am converting a pdf file to text and removing lines which have page number but the problem is that it leaving an empty space of 2 line.So i want to remove these spaces which have 2 or more empty line continuously but not if 1 line is empty.my code is :
// Open the file
FileInputStream fstream = new FileInputStream("C:\\Users\\Vivek\\Desktop\\novels\\Me1.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
String s=null;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
String pattern = "^[0-9]+[\\s]*$";
strLine=strLine.replaceAll(pattern, " ");
writeResult("C:\\Users\\Vivek\\Desktop\\novels\\doci.txt",strLine);
}
//Close the input stream
br.close();
}
public static void writeResult(String writeFileName, String text)
{
File log = new File(writeFileName);
try{
if(log.exists()==false){
System.out.println("We had to make a new file.");
log.createNewFile();
}
PrintWriter out = new PrintWriter(new FileWriter(log, true));
out.append(text );
out.println();
out.close();
}catch(IOException e){
System.out.println("COULD NOT LOG!!");
}
}
plz help me.
You can work with sequent empty line counter in your method like SkrewEverything suggested.
Or make a post-processing with regular expressions like this:
package testingThings;
import java.awt.Desktop;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class EmptyLinesReducer {
public Path reduceEmptyLines(Path in) throws UnsupportedEncodingException, IOException {
Path path = Paths.get("text_with_reduced_empty_lines.txt");
String originalContent = new String(Files.readAllBytes(in), "UTF-8");
String reducedContent = originalContent.replaceAll("(\r\n){2,}", "\n\n");
Files.write(path, reducedContent.getBytes());
return path;
}
public Path createFileWithEmptyLines() throws IOException {
Path path = Paths.get("text_with_multiple_empty_lines.txt");
PrintWriter out = new PrintWriter(new FileWriter(path.toFile()));
out.println("line1");
//empty lines
out.println();
out.println();
out.println();
out.println("line2");
//empty lines
out.println();
out.println("line3");
//empty lines
out.println();
out.println();
out.println();
out.println();
out.println();
out.println("line4");
out.close();
return path;
}
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
EmptyLinesReducer app = new EmptyLinesReducer();
Path in = app.createFileWithEmptyLines();
Path out = app.reduceEmptyLines(in);
// open the default program for this file
Desktop.getDesktop().open(out.toFile());
}
}
Related
I am trying to update a txt file in place, namely without creating a temp file or writing a file in a new file destination but I've tried all the solutions on stack overflow and none of these have worked so far.
It always give me an empty file as result. it simply delete all the content of the source file.
So I am trying to modify the following code, which takes two files as input, in order to take only one input (the file source) but without success.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
public class CopyFiles {
private static void copyFile(String sourceFileName, String destinationFileName) {
try (BufferedReader br = new BufferedReader(new FileReader(sourceFileName));
PrintWriter pw = new PrintWriter(new FileWriter(destinationFileName))) {
String line;
while ((line = br.readLine()) != null) {
line += " ENDING ";
pw.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String destinationFileName = "destination.csv";
String sourceFileName = "source.csv";
copyFile(sourceFileName, destinationFileName);
}
}
I want to do read from text file, if I find certain email then I want to remove the entire line.
So I want to remove email555#email.com
stuffherestuffemail555#email.comstuffstuff
otherrandomwordsinrandomorder
reandom word and spaces maybe # and charcters email555#email.com
APPLEPEARAPPLE
CATDOGCAT
CATDOGPEARemail555#email.comDogPear
To this
otherrandomwordsinrandomorder
APPLEPEARAPPLE
CATDOGCAT
Code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
class SendReq {
public static void main(String[] args) throws FileNotFoundException,
IOException{
File inputFile = new File("testfile.txt");
if (!inputFile.exists()){
inputFile.createNewFile();
}
File tempFile = new File("tempfile.txt");
if (!tempFile.exists()){
tempFile.createNewFile();
}
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = "NAMEOFEMAIL#EMAIL.com";
String currentLine;
while((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(lineToRemove)) continue;
writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close();
reader.close();
boolean successful = tempFile.renameTo(inputFile);
System.out.println(successful);
}
}
I made this homework exercise to read text from a text file and store it reversed into another new file. This is the code:
import java.util.*;
import java.io.*;
public class FileEcho {
File file;
Scanner scanner;
String filename = "words.txt";
File file1 ;
PrintWriter pw ;
void echo() {
try {
String line;
file = new File( filename);
scanner = new Scanner( file );
file1 = new File("brabuhr.txt");
pw = new PrintWriter(file1);
while (scanner.hasNextLine()) {
line = scanner.nextLine();
String s = new StringBuilder(line).reverse().toString();
pw.println(s);
}
scanner.close();
} catch(FileNotFoundException e) {
System.out.println( "Could not find or open file <"+filename+">\n"+e
);
}
}
public static void main(String[] args) {
new FileEcho().echo();
}
}
and here is a picture Picture here
The question is: why is the newly generated file decreased in size despite having the same characters but reversed?
Would be great if someone can explain it because even my professor didn't know why is that.
P.S; the context of the file is just some words from the dictionary.
Also in other students computers so the problem is not from my computer
The problem is that you never closed the output stream pw, so that any pending output isn't written to the underlying file. This may cause truncation of your file.
You should have closed the output stream with pw.close() in a finally, or in a try with resources.
try (pw = new PrintWriter(file1)) {
while (scanner.hasNextLine()) {
line = scanner.nextLine();
String s = new StringBuilder(line).reverse().toString();
pw.println(s);
}
}
Your implementation can be simplified to be the following:
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
public class FileEcho {
void echo() throws IOException {
try (PrintWriter pw = new PrintWriter("brabuhr.txt")) {
Files.lines(Paths.get("words.txt"))
.map(s -> new StringBuilder(s).reverse().toString())
.forEach(pw::println);
}
}
public static void main(String[] args) throws IOException {
new FileEcho().echo();
}
}
In this example I used a 'try-with-resources' to have the PrintWriter pw autoclosed.
The solution works and is capsulated and everything is fine, but .... I wonder if there is a better solution, maybe with new functions from Java 8 or some other improvements that can be done?
Main.java
package test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
class Main {
public static void main(String[] args) {
// zum speichern der Zeilen je Arrayfeld
List<String> lines = new ArrayList<String>();
// Inputdatei bestimmen
File file = new File("C:\\...\\test.txt");
// Inputdatei zeilenweise einlesen
ReadFile readfile = new ReadFile();
try {
// und in der ArrayList "lines" speichern
lines = readfile.byLine(file);
} catch (IOException e) {
e.printStackTrace();
}
// Inputdatei zeilenweise schreiben (neue Datei) mit Zeilennummer
WriteFile writefile = new WriteFile();
try {
writefile.byLine(lines);
} catch (IOException e) {
e.printStackTrace();
}
}
}
ReadFile.java
package test;
import java.util.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class ReadFile {
List<String> lines = new ArrayList<String>();
String line;
int lineNumber = 0;
protected List<String> byLine(File file) throws IOException {
// Inputdatei einlesen
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
// schauen ob Zeilenende erreicht wurde
while ((line = bufferedReader.readLine()) != null ) {
// Zeilen in Array speichern
lines.add(line);
}
// Reader schließen
bufferedReader.close();
// Array zurückgeben
return lines;
}
}
WriteFile.java
package test;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
public class WriteFile {
int lineNumber = 1;
protected void byLine(List<String> lines) throws IOException {
// neue Datei erstellen
PrintWriter writer = new PrintWriter("C:\\...\\test2.txt", "UTF-8");
// für jeden String (eingelesene Zeile) in der Arraylist
for( String line: lines) {
// Counter für die Zeilennummern
writer.println("*/ " + lineNumber + " /*" + " " + line);
lineNumber++;
}
// Writer schließen
writer.close();
}
}
Update
Didn't think file size was a concern given your posted example, but if it is you can read your file and write to a temp file all at once and then delete your old file and rename the new file to the old file name.
public static void main(String[] args) throws Exception {
insertLineNumbersIntoFile("C:\\...\\test2.txt");
}
private static void insertLineNumbersIntoFile(String filePath) throws Exception {
String tempFile = "temp.txt";
// Open reader and writer
FileWriter writer = new FileWriter(tempFile);
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
// Read lines
int lineNumber = 1;
String line = "";
while ((line = bufferedReader.readLine()) != null) {
// Insert line number
line = "*/ " + lineNumber + " /* " + line;
// Write new line to new file
writer.write(line + "\r\n");
// Increment line number
lineNumber++;
}
// Close reader and writer
bufferedReader.close();
writer.close();
// Delete old file and rename new file to old
File oldFile = new File(filePath);
File newFile = new File(tempFile);
oldFile.delete();
newFile.renameTo(oldFile);
}
If you're not dead set on having a reading & writing class, you could shorten all of your code to this...
public static void main(String[] args) throws Exception {
List<String> lines = Files.readAllLines(Paths.get("C:\\...\\test2.txt"));
FileWriter writer = new FileWriter("C:\\...\\test2.txt");
for (int i = 0; i < lines.size(); i++) {
lines.set(i, "*/ " + (i+1) + " /* " + lines.get(i));
writer.write(lines.get(i) + "\r\n");
}
writer.close();
}
Maybe the class will suite you (pay attention to getLineNumber() method):
https://docs.oracle.com/javase/7/docs/api/java/io/LineNumberReader.html
For example we have a .txt file:
Name smth
Year 2012
Copies 1
And I want to replace it with that:
Name smth
Year 2012
Copies 0
Using java.io.*.
Here is the code that does that. Let me know if you have any question.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.Map;
public class Test2 {
Map<String, String> someDataStructure = new LinkedHashMap<String, String>();
File fileDir = new File("c:\\temp\\test.txt");
public static void main(String[] args) {
Test2 test = new Test2();
try {
test.readFileIntoADataStructure();
test.writeFileFromADataStructure();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
private void readFileIntoADataStructure() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(fileDir)));
String line;
while ((line = in.readLine()) != null) {
if (line != null && !line.trim().isEmpty()) {
String[] keyValue = line.split(" ");
// Do you own index and null checks here this is just a sample
someDataStructure.put(keyValue[0], keyValue[1]);
}
}
in.close();
}
private void writeFileFromADataStructure() throws IOException {
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fileDir)));
for (String key : someDataStructure.keySet()) {
// Apply whatever business logic you want to apply here
myBusinessMethod(key);
out.write(key + " " + someDataStructure.get(key) + "\n");
out.append("\r\n");
out.append("\r\n");
}
out.flush();
out.close();
}
private String myBusinessMethod(String data) {
if (data.equalsIgnoreCase("Copies")) {
someDataStructure.put(data, "0");
}
return data;
}
}
Read your original text file line by line and separate them into string tokens delimited by spaces for output, then when the part you want replaced is found (as a string), replace the output to what you want it to be. Adding the false flag to the filewrite object ("filename.txt", false) will overwrite and not append to the file allowing you to replace the contents of the file.
this is the code to do that
try {
String sCurrentLine;
BufferedReader br = new BufferedReader(new FileReader("yourFolder/theinputfile.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("yourFolder/theinputfile.txt" , false));
while ((sCurrentLine = br.readLine()) != null) {
if(sCurrentLine.indexOf("Copies")>=0){
bw.write("Copies 0")
}
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close()bw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
hopefully that help