import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class Test {
List<String> knownWordsArrayList = new ArrayList<String>();
List<String> wordsArrayList = new ArrayList<String>();
List<String> newWordsArrayList = new ArrayList<String>();
String toFile = "";
public void readKnownWordsFile() {
try {
FileInputStream fstream2 = new FileInputStream("knownWords.txt");
BufferedReader br2 = new BufferedReader(new InputStreamReader(fstream2, "UTF-8"));
String strLine;
while ((strLine = br2.readLine()) != null) {
knownWordsArrayList.add(strLine.toLowerCase());
}
HashSet h = new HashSet(knownWordsArrayList);
// h.removeAll(knownWordsArrayList);
knownWordsArrayList = new ArrayList<String>(h);
// for (int i = 0; i < knownWordsArrayList.size(); i++) {
// System.out.println(knownWordsArrayList.get(i));
// }
} catch (Exception e) {
// TODO: handle exception
}
}
public void readFile() {
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("Smallville 4x02.de.srt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
String numberedLineRemoved = "";
String strippedInput = "";
String[] words;
String trimmedString = "";
String temp = "";
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
temp = strLine.toLowerCase();
// Print the content on the console
numberedLineRemoved = numberedLine(temp);
strippedInput = numberedLineRemoved.replaceAll("\\p{Punct}", "");
if ((strippedInput.trim().length() != 0) || (!strippedInput.contains("")) || (strippedInput.contains(" "))) {
words = strippedInput.split("\\s+");
for (int i = 0; i < words.length; i++) {
if (words[i].trim().length() != 0) {
wordsArrayList.add(words[i]);
}
}
}
}
HashSet h = new HashSet(wordsArrayList);
h.removeAll(knownWordsArrayList);
newWordsArrayList = new ArrayList<String>(h);
// HashSet h = new HashSet(wordsArrayList);
// wordsArrayList.clear();
// newWordsArrayList.addAll(h);
for (int i = 0; i < newWordsArrayList.size(); i++) {
toFile = newWordsArrayList.get(i) + ".\n";
// System.out.println(newWordsArrayList.get(i) + ".");
System.out.println();
}
System.out.println(newWordsArrayList.size());
// Close the input stream
in.close();
} catch (Exception e) {// Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
public String numberedLine(String string) {
if (string.matches(".*\\d.*")) {
return "";
} else {
return string;
}
}
public void writeToFile() {
try {
// Create file
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write(toFile);
// Close the output stream
out.close();
} catch (Exception e) {// Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
public static void main(String[] args) {
Test test = new Test();
test.readKnownWordsFile();
test.readFile();
test.writeToFile();
}
}
How can I read äöüß from file?
Would the string.toLowercase() handle these properly as well?
And when I go to print words containing any of äöüß, how can I print the word properly?
When I print to console I get
Außerdem
weiß
for Außerdem
weiß
How can I fix this?
I tried:
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
But now I'm getting aufkl?ren instead of aufklären and its messing up in other places as well.
Updated the code to see if it would print on the file properly, but I'm just getting one in the file.
You need to read files using the charset which was used to create the file. If you're on a windows machine, that's probably cp1252. So:
BufferedReader br = new BufferedReader(new InputStreamReader(in, "Cp1252"));
If that doesn't work, most text editors are capable of telling you what encoding is used for a given document.
Related
this is my code, im trying to compare two .csv files and match them and save the common pain in another file. How do i do it?
This is the cotnent of item_no.csv file
1
2
3
4
5
This is the content of item_desc.csv file
1,chocolate,100
2,biscuit,20
3,candy,10
4,lollipop,5
5,colddrink,50
6,sandwitch,70
EDIT This is the expected output:
1,chocolate,100
2,biscuit,20
3,candy,10
4,lollipop,5
5,colddrink,50
This is my code:
package fuu;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
public class Demo {
public static void main(String[] args) throws ParseException, IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new FileReader("/home/yotta/eclipse/workspace/Test/WebContent/doc/item_no.csv"));
BufferedReader br1 = new BufferedReader(new FileReader("/home/yotta/eclipse/workspace/Test/WebContent/doc/item_desc.csv"));
String line = null;
String line1 = null;
String line2 = null;
String[] str=null;
String[] str1=null;
try {
while((line = br.readLine())!=null){
str = line.split(",");
System.out.println(str[0]);
}
while((line1 = br1.readLine())!=null){
str1 = line1.split(",");
System.out.println(str1[0]+" "+str1[1]+" "+str1[2]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
You could separate the different steps.
public class Demo {
public static void main(String[] args) throws IOException {
Map<String, String> descMap = new HashMap<>();
String line;
// read all item descriptions
try (BufferedReader br1 = new BufferedReader(new FileReader("item_desc.csv"))) {
while ((line = br1.readLine()) != null) {
int itemNbrSeparator = line.indexOf(',');
String itemNbr = line.substring(0, itemNbrSeparator);
descMap.put(itemNbr, line);
}
}
List<String> matched = new ArrayList<>();
// read the item numbers and store each matched
try (BufferedReader br = new BufferedReader(new FileReader("item_no.csv"))) {
while ((line = br.readLine()) != null) {
if (descMap.containsKey(line)) {
System.out.println(descMap.get(line));
matched.add(descMap.get(line));
}
}
}
// output all matched
Path outFile = Paths.get("item_match.csv");
Files.write(outFile, matched, Charset.defaultCharset(), new LinkOption[0]);
}
}
One way is this
List<String> lines1 = new ArrayList<String>();
while ((line = br.readLine()) != null) {
str = line.split(",");
lines1.add(line);
System.out.println(str[0]);
}
List<String> lines2 = new ArrayList<String>();
while ((line = br1.readLine()) != null) {
str = line.split(",");
System.out.println(str[0]);
if(lines1.contains(str[0])){
lines2.add(line);
}
}
for (String l : lines1) {
System.out.println(l);
}
As per the captioned subject, I am not able to delete String from a text file on deployment in tomcat. Although it works absolutely fine in netbeans or eclipse.
When application is deployed it always says 'could not delete file'. Also I am not able to manually delete the file as I get 'Cannot delete file: It is in use by some other programs'.
But I am able to append String to this same text file.
This is the code that I am using to delete String from the file.
package com.pro.model;
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;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class RemoveLine {
private String inputFileName = "D:\\Marquee\\Copy of Scroll.txt";
public boolean deleteData(String msg, int pos) throws IOException {
//List msg_remove = null;
System.out.println("msg :" + msg);
BufferedReader reader = null;
try {
File inputFile = new File(inputFileName);
File tempFile = new File("D:\\myTempFile.txt");
reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = msg;
String currentLine;
while ((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
String[] trimmedLine = currentLine.split("\\|");
System.out.println("currentLine :" + currentLine);
System.out.println("trimmedLine :" + trimmedLine.length);
for (int i = 0; i < trimmedLine.length; i++) {
int k = i + 1;
System.out.println("Calculating the size of field " + k);
//writer.write("Column " + k + " is " + trimmedLine[i].length());
// is.flush();
// is.newLine();
System.out.println("trimmedLine[i] :" + trimmedLine[i]);
//writer.write(trimmedLine[i] + "|");
System.out.println("trimmedLine[i].equals(lineToRemove) :" + trimmedLine[i].equals(lineToRemove));
//if (trimmedLine[i].equals(lineToRemove)) {
if (i == pos) {
System.out.println("trimmedLine in if:" + trimmedLine[i]);
//writer.flush();
//trimmedLine.replaceAll(currentLine, "");
continue;
}
System.out.println("currentLine :" + currentLine);
//writer.write(currentLine + "|");
writer.write(trimmedLine[i] + "|");
}
}
writer.flush();
reader.close();
writer.close();
if (!inputFile.delete()) {
System.out.println("Could not delete file");
return false;
}
//Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inputFile)) {
System.out.println("Could not rename file");
return false;
}
return true;
//boolean successful = tempFile.renameTo(inputFile);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
return false;
} finally {
try {
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public List readText() throws FileNotFoundException, IOException {
BufferedReader reader = null;
List msg_after_removed = new ArrayList();
try {
File inputFile = new File(inputFileName);
//File tempFile = new File("C:\\myTempFile.txt");
reader = new BufferedReader(new FileReader(inputFile));
//msg_after_removed = new ArrayList();
if (inputFile.exists()) {
//data = msg1;
FileWriter fileWritter = new FileWriter(inputFile.getPath(), true);
System.out.println("file.getName():" + inputFile.getName());
System.out.println("file.getName():" + inputFile.getPath());
reader = new BufferedReader(new FileReader(inputFile));
String currentLine;
while ((currentLine = reader.readLine()) != null) {
String[] trimmedLine = currentLine.split("\\|");
for (int i = 0; i < trimmedLine.length; i++) {
Bean getdata = new Bean();
System.out.println("trimmedLine in read ==" + trimmedLine[i] + i + "=length=" + trimmedLine.length);
getdata.setMsg(trimmedLine[i]);
String arr[] = trimmedLine[i].split(":");
System.out.println("arr[] in read" + arr[1]);
getdata.setDate(arr[0]);
getdata.setMsg(arr[1]);
msg_after_removed.add(getdata);
//reader.close();
}
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
Bean getdata = new Bean();
getdata.setDate("0");
getdata.setMsg("0");
msg_after_removed.add(getdata);
System.out.println("in catch");
//reader.close();
//return false;
} finally {
if (reader != null) {
reader.close();
}
}
return msg_after_removed;
}
}
I thought maybe Read stream is not closing properly & that is why I am not able to delete/save file manually. But if that was the case, I should also not be able to append String to the file.
Any help is appreciated.
In my below code I wanted to replace the text "DEMO" with "Demographics" but instead of replacing the text it deletes the entire content of the text file.
Contents inside the file:
DEMO
data
morning
PS: I'm a beginner in java
package com.replace.main;
import java.io.*;
public class FileEdit {
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
String readLine, replacedData;
try {
bw = new BufferedWriter(
new FileWriter(
"Demg.ctl"));
br = new BufferedReader(
new FileReader(
"Demg.ctl"));
System.out.println(br.readLine()); //I Get Null Printed Here
while ((readLine = br.readLine())!= null) {
System.out.println("Inside While Loop");
System.out.println(readLine);
if (readLine.equals("DEMO")) {
System.out.println("Inside if loop");
replacedData = readLine.replaceAll("DEMO","Demographics");
}
}
System.out.println("After While");
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
You open a Writer to your file, but you don't write anything. This means that your file is replaced with an empty file.
Besides this you also need to close your writer, not just the reader.
And last but not least, your if condition is wrong.
if (readLine.equals("DEMO")) {
should read
if (readLine.contains("DEMO")) {
Otherwise it would only return true if your line contained "DEMO" but nothing else.
I'm updating the answer to my own question.
package com.replace.main;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileEdit
{
public static void main(String args[])
{
try
{
BufferedReader reader = new BufferedReader(new FileReader("Demg.ctl"));
String readLine = "";
String oldtext = "";
while((readLine = reader.readLine()) != null)
{
oldtext += readLine + "\r\n";
}
reader.close();
// To replace the text
String newtext = oldtext.replaceAll("DEMO", "Demographics");
FileWriter writer = new FileWriter("Demg.ctl");
writer.write(newtext);
writer.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
I am trying to read in a technical paper, separate all the sentences, use a filter to find key terms and phrases in the sentences, and then create my own abstract.
What I have so far is two BufferedReaders reading a text file with a paragraph in it, and my filter being read in. Each line is then being stored into an ArrayList and printed to the console to test if they are being read correctly.
I want to know if I am approaching this the correct way by using a BufferedReader instead of a Scanner. I just want to be able to print out all the sentences after a '.' (dot), a '!' (exclamation-point), or a '?' (question-mark) for right now, so I know that the file is being read correctly.
This is my code so far:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.io.*;
import java.util.Scanner;
public class Filtering {
public static void main(String[] args) throws IOException {
ArrayList<String> lines1 = new ArrayList<String>();
ArrayList<String> lines2 = new ArrayList<String>();
try {
FileInputStream fstream1 = new FileInputStream("paper.txt");
FileInputStream fstream2 = new FileInputStream("filter2.txt");
DataInputStream inStream1 = new DataInputStream (fstream1);
DataInputStream inStream2 = new DataInputStream (fstream2);
BufferedReader br1 = new BufferedReader(
new InputStreamReader(inStream1));
BufferedReader br2 = new BufferedReader(
new InputStreamReader(inStream2));
String strLine1;
String strLine2;
while ((strLine1 = br1.readLine()) != null) {
lines1.add(strLine1);
}
while ((strLine2 = br2.readLine()) != null) {
lines2.add(strLine2);
}
inStream1.close();
inStream2.close();
}
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
System.out.println(lines1);
System.out.println(lines2);
}
}
It is a good practice to use a BufferedReader to read any File as it will buffer the File instead of accessing each bytes one by one
The DataInputStream is not needed
You should specify a character encoding in your InputStreamReader
You could accumulate all your string in a StringBuilder so that you have the whole text in a single reference
You may want to look into BreakIterator to split your text into sentences. Have a look at getSentenceInstance().
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.BreakIterator;
public class Filtering {
public static void main(String[] args) throws IOException {
File paperFile = new File("paper.txt");
File filterFile = new File("filter2.txt");
// If you want you could roughly initiate the stringbuilders to their
// approximate future size
StringBuilder paper = new StringBuilder();
StringBuilder filter2 = new StringBuilder();
FileInputStream fstream1 = null;
FileInputStream fstream2 = null;
try {
fstream1 = new FileInputStream(paperFile);
fstream2 = new FileInputStream(filterFile);
BufferedReader br1 = new BufferedReader(new InputStreamReader(fstream1, "UTF-8"));
BufferedReader br2 = new BufferedReader(new InputStreamReader(fstream2, "UTF-8"));
String strLine1;
String strLine2;
while ((strLine1 = br1.readLine()) != null) {
paper.append(strLine1).append('\n');
}
while ((strLine2 = br2.readLine()) != null) {
filter2.append(strLine2).append('\n');
}
}
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
} finally {
if (fstream1 != null) {
fstream1.close();
}
if (fstream2 != null) {
fstream2.close();
}
}
String paperString = paper.toString();
String filterString = filter2.toString();
System.out.println(paperString);
System.out.println(filterString);
// To break it into sentences
BreakIterator boundary = BreakIterator.getSentenceInstance();
boundary.setText(paperString);
int start = boundary.first();
for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
System.out.println(paper.substring(start, end));
}
}
}
I wrote this method, which is never ending. It isn't printing what I'm passing, why?
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
class Main {
public void readFromConsole() {
ArrayList<String> wholeInput= new ArrayList <String>();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
try {
String line = null;
while (!(line = br.readLine()).equals(" ")){
wholeInput.add(line);
}
}
catch(IOException e){
e.printStackTrace();
}
for (int i =0; i<wholeInput.size();i++){
System.out.println(wholeInput.get(i));
}
}
}
" " is not an empty line, it is a space. Try ""
while (!(line = br.readLine()).trim().equals("")){