having a little bit of an issue. I am looping through a file where by I want to filter out a series of texts and concatenate them at the end of each loop, which then ultimately end up ordering i.e. during the loop phase it does the following:
String A = "A /n"
String A = "A /n U /n"
String A = "A /n U /n B /n"
etc...
The output will be
A
U
B
however i want it to be
A
B
U
I have so far done the following:
public static void organiseFile() throws FileNotFoundException {
ArrayList<String> lines = new ArrayList<>();
ArrayList<String> order = new ArrayList<>();
String directory = "C:\\Users\\xxx\\Desktop\\Files\\ex1";
Scanner fileIn = new Scanner(new File(directory + "_ordered.txt"));
PrintWriter out = new PrintWriter(directory + "_orderesqsd.txt");
String otherStates = "";
while (fileIn.hasNextLine() == true) {
lines.add(fileIn.nextLine());
System.out.println("Organising...");
}
Collections.sort(lines);
for (String output : lines) {
if (output.contains("[EVENT=agentStateEvent]")) {
out.println(output + "\n");
out.println(otherStates + "\n");
otherStates = "";
}
else {
otherStates += output+ "\n";
}
out.close();
}
Now this does output fine, however, with regards to the "otherStates", i want to get this in a numeric order, and the best way I know is using Collections, however this is for arrays. I am unsure how to go about modifying the "otherStates" part of the code to cater for an array that concatanetates the string and then be able to order them accordingly. Any ideas
Hard to give a correct solution without input file data . Just try the below code. At the very least it should give you some ideas on how to solve the issue
public static void organiseFile() throws FileNotFoundException {
ArrayList<String> lines = new ArrayList<>();
ArrayList<String> order = new ArrayList<>();
String directory = "C:\\Users\\xxx\\Desktop\\Files\\ex1";
Scanner fileIn = new Scanner(new File(directory + "_ordered.txt"));
PrintWriter out = new PrintWriter(directory + "_orderesqsd.txt");
String otherStates = "";
ArrayList<String> otherStates_duplicate = new ArrayList<>();
String ordered_new_string.;
while (fileIn.hasNextLine() == true) {
lines.add(fileIn.nextLine());
System.out.println("Organising...");
}
Collections.sort(lines);
for (String output : lines) {
if (output.contains("[EVENT=agentStateEvent]")) {
out.println(output + "\n");
out.println(otherStates + "\n");
otherStates = "";
}
else {
otherStates += output+ "\n";
otherStates_duplicate.add(output);
}
Collections.sort(otherStates_duplicate); // Now this should have a sorted list
//if you need a string instead of an arraylist use code below in addition
for(String s:otherStates_duplicate){
ordered_new_string += s + "\n";
}
/*
I have not printed or stored the string ordered_new_string as it is not
clear to me what you want. print/write to a file and check
if ordered_new_string is what your required
*/
out.close();
}
Related
I have a file containing some data that looks like this.
[Todays date] Some text
some more text
even more text
[A different date] Some text
[Another different date] More text
Final text block
I'd like to read this into a List<String> if possible but I don't want to have it exactly like the file. Ideally the List<String> would look like.
[Todays date] some text some more text even more text
[A different date] some text
[Another different date] More text Final text block
If the line of text doesn't start with a squared bracket I want to just concatenate that line to the above line. I can read it in normally using this code. I tried to use String.startsWith but I couldn't figure it out.
List<String> testList = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader("SystemOut.log"));
String line = br.readLine();
while (line!=null) {
testList.add(line);
line=br.readLine();
}
br.close();
I'm looking for either a change to this method that will make it read it in to my desired format or maybe a method that will act on my List<String> and sort this problem. Thanks
A better solution (3rd attempt) which (hopefully) will work better with large files as it avoids reading the whole file into buffer:
public static void main(String[] args) throws IOException {
String input = "[04/06/2021] Some text\n" +
"some more text\n" +
"even more text\n" +
"[01/01/2020] Some text \n" +
"[31/12/2020] More text\n" +
"Final text block";
List<String> testList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new StringReader(input))) {
br.lines().forEach(
line -> {
if (testList.isEmpty() || line.startsWith("[")) {
testList.add(line + " ");
} else {
testList.set(
testList.size() - 1,
testList.get(testList.size() - 1) + line + " ");
}
}
);
}
testList.forEach(System.out::println);
}
I came up with this tedious method:
public static void main(String[] args) throws IOException {
String input = "[04/06/2021] Some text\n" +
"some more text\n" +
"even more text\n" +
"[01/01/2020] Some text \n" +
"[31/12/2020] More text\n" +
"Final text block";
List<String> testList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new StringReader(input))) {
String nextLine = br.readLine();
StringBuilder currentLine = new StringBuilder(nextLine + " ");
while (nextLine != null) {
nextLine = br.readLine();
if (nextLine != null) {
if (nextLine.startsWith("[")) {
testList.add(currentLine.toString());
currentLine = new StringBuilder();
}
currentLine.append(nextLine).append(" ");
}
}
if (currentLine.length() > 0) {
testList.add(currentLine.toString());
}
}
testList.forEach(System.out::println);
}
If you can move away from your loop, a better/simpler approach would be:
public static void main(String[] args) throws IOException {
String input = "[04/06/2021] Some text\n" +
"some more text\n" +
"even more text\n" +
"[01/01/2020] Some text \n" +
"[31/12/2020] More text\n" +
"Final text block";
List<String> testList = new ArrayList<>();
String[] inputs = input.split("\\n");
StringBuilder currentLine = new StringBuilder(inputs[0] + " ");
for (int i = 1; i < inputs.length; i++) {
if (inputs[i].startsWith("[")) {
testList.add(currentLine.toString());
currentLine = new StringBuilder();
}
currentLine.append(inputs[i]).append(" ");
}
testList.add(currentLine.toString());
testList.forEach(System.out::println);
}
Output:
[04/06/2021] Some text some more text even more text
[01/01/2020] Some text
[31/12/2020] More text Final text block
I created the following code to read a CSV-file:
public void read(String csvFile) {
try {
File file = new File(csvFile);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = "";
String[] tempArr;
while((line = br.readLine()) != null) {
tempArr = line.split(ABSTAND);
anzahl++;
for(String tempStr : tempArr) {
System.out.print(tempStr + " ");
}
System.out.println();
}
br.close();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
I have a CSV with more than 300'000 lines that look like that:
{9149F314-862B-4DBC-B291-05A083658D69};Gebaeude;TLM_GEBAEUDE;;Schiessstand;{41C949A2-9F7B-41EE-93FD-631B76F2176D};Altdorf 300m;offiziell;Hochdeutsch inkl. Lokalsprachen;Einfacher Name;;684600;295930;400
How can I now only get the some parts out of that? I only need the bold/italic parts to work with.
Without further specifying what your requirements/input limitations are the following should work within your loop.
String str = "{9149F314-862B-4DBC-B291-05A083658D69};Gebaeude;TLM_GEBAEUDE;;Schiessstand;{41C949A2-9F7B-41EE-93FD-631B76F2176D};Altdorf 300m;offiziell;Hochdeutsch inkl. Lokalsprachen;Einfacher Name;;684600;295930;400";
String[] arr = str.split("[; ]", -1);
int cnt=0;
// for (String a : arr)
// System.out.println(cnt++ + ": " + a);
System.out.println(arr[6] + ", " + arr[15] + ", " + arr[16]);
Note that this assumes your delimiters are either a semicolon or a space and that the fields desired are in the fix positions (6, 15, 16).
Result:
Altdorf, 684600, 295930
I have two related text files shown for example in data1.txt and data2.txt. I want to merge the two files to create result.txt. Any idea how to go about this?
data1.txt
books, 3
Shelf, 5
groceries,6
books, 1
Shelf, 2
data2.txt
books,2
shelf,3
groceries,1
result.txt
books, 3, 2
Shelf, 5,3
groceries,6,1
books, 1,2
Shelf, 2, 3
this is a example for you.first you need to add values to 2d list from data2 text file.and then when line is null in file2 you can get mapping value relative to it's text from that list .so i have a method which will return back the mapping value for a String .code is little long than i thought .i post only relevant methods here.This is link to complete class file
public void marged(){
try {
BufferedReader br1 = null;
BufferedReader br2 = null;
String line1;
String line2;
ArrayList<ArrayList<String>> arrayList = new ArrayList<>();
br1 = new BufferedReader(new FileReader("C:\\Users\\Madhawa.se\\Desktop\\workingfox\\data1.txt"));
br2 = new BufferedReader(new FileReader("C:\\Users\\Madhawa.se\\Desktop\\workingfox\\data2.txt"));
while ((line1 = br1.readLine()) != null) {
String[] split1 = line1.split(",");
String line1word = split1[0].trim();
String line1val = split1[1].trim();
line2 = br2.readLine();
if (line2 != null) {
String[] split2 = line2.trim().split(",");
String line2word = split2[0].trim();
String line2val = split2[1].trim();
ArrayList<String> list = new ArrayList();
list.add(line2word);
list.add(line2val);
arrayList.add(list);
if (line1word.equalsIgnoreCase(line2word)) {
String ok = line1word + "," + line1val + "," + line2val;
System.out.println(ok);
}
} else {
String ok = line1word + "," + line1val + "," + doesexist(arrayList, line1word);
System.out.println(ok);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
this is the method return mapping value
public String doesexist(ArrayList<ArrayList<String>> arrayList, String s) {
for (int i = 0; i < arrayList.size(); i++) {
String get = arrayList.get(i).get(0);
if (get.trim().equalsIgnoreCase(s.trim())) {
return arrayList.get(i).get(1);
}
}
return "-1";
}
output>>
books,3,2
Shelf,5,3
groceries,6,1
books,1,2
Shelf,2,3
Simply add files into an array of File object then read it using loop.
File []files = new Files[amountOfFiles];
//initialize array elements
for(File f:files)
{
//read each file and store it into string variable
}
//finally write the string variable into result.txt file.
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;
public class SOQ21
{
public SOQ21()
{
merge();
}
public void merge()
{
try
{
String firstfile = "data1.txt";
FileReader fr1 = new FileReader(firstfile);
BufferedReader bfr1 = new BufferedReader(fr1);
String secondfile = "data2.txt";
FileReader fr2 = new FileReader(secondfile);
BufferedReader bfr2 = new BufferedReader(fr2);
/*
^^^ Right here is how you get the files and accompanying BufferedReaders
to handle them
*/
//next, using the readLine() method from the Java API, read each line
//for the first file
//then, separate by taking the words into an ArrayList and storing the
//numbers as Strings in a String[] of equal length of the ArrayList
//Do the same for the second file
//Then, if the word of ArrayList 1 matches the word of ArrayList 2,
//append the String numbers from String[] 2 to String[] 1
//DONE! :)
}
catch(FileNotFoundException ex)
{
//handle how you want
}
}
public static void main(String[] args)
{
SOQ21 soq = new SOQ21();
}
}
The comments I made should answer most of your questions. Lastly, I would pay special attention to the exceptions, I'm not entirely sure how you wanted to deal with that, but make sure you fill it with SOMETHING!
Can any one suggest, how to use string-tokens in java, to read all data in a file, and display only some of its contents. Like, if i have
apple = 23456, mango = 12345, orange= 76548, guava = 56734
I need to select apple, and the value corresponding to apple should be displayed in the output.
This is the code
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
public class ReadFile {
public static void main(String[] args) {
try {
String csvFile = "Data.txt";
//create BufferedReader to read csv file
BufferedReader br = new BufferedReader(new FileReader(csvFile));
String line = "";
StringTokenizer st = null;
int lineNumber = 0;
int tokenNumber = 0;
//read comma separated file line by line
while ((line = br.readLine()) != null) {
lineNumber++;
//use comma as token separator
st = new StringTokenizer(line, ",");
while (st.hasMoreTokens()) {
tokenNumber++;
//display csv values
System.out.print(st.nextToken() + " ");
}
System.out.println();
//reset token number
tokenNumber = 0;
}
} catch (Exception e) {
System.err.println("CSV file cannot be read : " + e);
}
}
}
this is the file I'm working on :
ImageFormat=GeoTIFF
ProcessingLevel=GEO
ResampCode=CC
NoScans=10496
NoPixels=10944
MapProjection=UTM
Ellipsoid=WGS_84
Datum=WGS_84
MapOriginLat=0.00000000
MapOriginLon=0.00000000
ProdULLat=18.54590200
ProdULLon=73.80059300
ProdURLat=18.54653200
ProdURLon=73.90427600
ProdLRLat=18.45168500
ProdLRLon=73.90487900
ProdLLLat=18.45105900
ProdLLLon=73.80125300
ProdULMapX=373416.66169100
ProdULMapY=2051005.23286800
ProdURMapX=384360.66169100
ProdURMapY=2051005.23286800
ProdLRMapX=373416.66169100
ProdLRMapY=2040509.23286800
ProdLLMapX=384360.66169100
ProdLLMapY=2040509.23286800
Out of this, i need to display only the following :
NoScans
NoPixels
ProdULLat
ProdULLon
ProdLRLat
ProdLRLon
public class Test {
public String getValue(String str, String strDelim, String keyValueDelim, String key){
StringTokenizer tokens = new StringTokenizer(str, strDelim);
String sentence;
while(tokens.hasMoreElements()){
sentence = tokens.nextToken();
if(sentence.contains(key)){
return sentence.split(keyValueDelim)[1];
}
}
return null;
}
public static void main(String[] args) {
System.out.println(new Test().getValue("apple = 23456, mango = 12345, orange= 76548, guava = 56734", ",", "=", "apple"));
}
}
" I noticed you have edited your question and added your code. for your new version question you can still simply call method while reading the String from the file and get your desire value ! "
I have written code assuming you have already stored data from file to a String,
public static void main(String[] args) {
try {
String[] CONSTANTS = {"apple", "guava"};
String input = "apple = 23456, mango = 12345, orange= 76548, guava = 56734";
String[] token = input.split(",");
for(String eachToken : token) {
String[] subToken = eachToken.split("=");
// checking whether this data is required or not.
if(subToken[0].trim().equals(CONSTANTS[0]) || subToken[0].trim().equals(CONSTANTS[1])) {
System.out.println("No Need to do anything");
} else {
System.out.println(subToken[0] + " " + subToken[1]);
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
read a complete line using bufferedreader and pass it to stringtokenizer with tokenizer as "="[as you mentioned in your file].
for more please paste your file and what you have tried so far..
ArrayList<String> list = new ArrayList<String>();
list.add("NoScans");
list.add("NoPixels");
list.add("ProdULLat");
list.add("ProdULLon");
list.add("ProdLRLat");
list.add("ProdLRLon");
//read a line from a file.
while ((line = br.readLine()) != null) {
lineNumber++;
//use 'equal to' as token separator
st = new StringTokenizer(line, "=");
//check for tokens from the above string tokenizer.
while (st.hasMoreTokens()) {
String key = st.nextToken(); //this will give the first token eg: NoScans
String value = st.nextToken(); //this will give the second token eg:10496
//check the value is present in the list or not. If it is present then print
//the value else leave it as it is.
if(list.contains(key){
//display csv values
System.out.print(key+"="+ " "+value);
}
}
Here is my code:
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
public class Filter{
Message myMessage;
Scanner input;
Scanner input2;
String sender;
String subject;
String emailMIN;
String line;
String line2;
ArrayList<String> blacklist = new ArrayList<String>();
ArrayList<String> keywords = new ArrayList<String>();
ArrayList<String> subjectWords = new ArrayList<String>();
ArrayList<String> emails = new ArrayList<String>();
//String[] lines;
File SpamMessage;
File inFile;
File inFile2;
File tempFile;
String[] lines;
public Filter(Message m,String blacklistFile, String keywordFile, String Spam)throws IOException{
inFile = new File(blacklistFile);
inFile2 = new File(keywordFile);
input = new Scanner (inFile);
input2 = new Scanner (inFile2);
myMessage =m;
SpamMessage=new File(Spam);
}
public void filter() throws IOException{
PrintWriter output = new PrintWriter(SpamMessage);
while(input.hasNextLine()){
line = input.nextLine();
//System.out.println(line);
if(line!=null)
blacklist.add(line);
}
while(input2.hasNextLine()){
line2 = input2.nextLine();
//System.out.println(line2);
if(line!=null)
keywords.add(line2);
}
emails=myMessage.getEmails();
// System.out.println(emails.size() + emails.get(1));
for(int i = 0; i < emails.size(); i++){
// boolean isSpam = false;
lines = emails.get(i).split("\n");
// System.out.println(lines[5] + lines[7]);
sender = lines[2].substring(lines[2].indexOf('<'), lines[2].indexOf('>'));
//` System.out.println(sender);
emailMIN = lines[6].substring(lines[6].indexOf('<'), lines[6].indexOf('>'));
// System.out.println(emailMIN);
for(int j =0; j<lines.length; j++)
{
if(j==2)
{
for(String blacklist2: blacklist)
{
// System.out.println(blacklist2);
if(lines[j].contains(blacklist2))
{
output.println(emailMIN);
}
// output.close();
}
}
if(j==5 || j>=7)
{
// System.out.println(keywords.size());
for(String keywords2: keywords)
{
// System.out.println(keywords2);
if(lines[j].contains(keywords2))
{
output.println(emailMIN);
}
// output.close();
}
}
//addKeywords();
}
}
output.close();
addKeywords();
}
public void addKeywords() throws IOException
{
tempFile = new File("tempFile.txt");
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
for(int i=0; i<lines.length; i++)
{
if(i==5){
String[] words = lines[i].split(" ");
for(String word: words){
if(word.length()>=6){
subjectWords.add(word +"\n");
//System.out.println(subjectWords);
}
}
keywords.addAll(subjectWords);
pw.println(keywords);
}
}
pw.close();
if (!inFile2.delete()) {
//System.out.println("Could not delete file");
return;
}
// Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile2)){
//System.out.println("Could not rename file");
}
}
}
I'm trying to update the list of words in the keywords txt file right now it does update it but it puts it in the format [generic, pharmacy, little, inside]
Which is wrong because then if I run my code again it is searching if the file contains [generic, pharmacy, little, inside] and I need it to search for every word not the plus a comma or brace. So basically I want it to copy the words in a list format like this
generic
pharmacy
little
inside
That way it searches for each individual word. I figured out how to do this part. Now, how do I add the senders to a different text file? Also is there a way to modify this so it doesn't add the same keywords twice? Thanks
It is because you are writing an array to the file which causes the toString method of it to be called. Write every single item instead.
Instead of pw.println(keywords);
Do:
for (String keyword : keywords)
{
pw.println(keyword.trim());
}
Or, if every word contains \n already, this should work
for (String keyword : keywords)
{
pw.print(keyword);
}
Instead of doing:
pw.println(keywords);
you should instead loop through the array and add each line individually.
for(int i = 0; i < keywords.length; i++) {
pw.println(keywords[i]);
}
That was because you are printing an ArrayList object. In your code, keywords is instance of the List and which would you give you an output of [aa,bb] . More over you would get duplicate words since these list instance are class variables, and printed inside a loop
keywords.addAll(subjectWords);
pw.println(keywords);
Either you can loop around keywords outside the for loop or print the word before adding to list.