EDITED
Im trying to split a text into an Array. I have a .txt made by different text, like a collection of texts. I need the whole text of each different text in the .txt in a position of the Array.
Im recovering the text from the file with a JFileChooser. And then im trying to process it with "regex" String.Split and then trying to print it. "The first part of the FileChooser it works, but when trying to separate the text in the Array i dont know if its working, because the System.out does not print the expected Array whit all the texts."
This is an example of the .txt each text is separated by a "*TEXT".
*TEXT 017 01/04/63 PAGE 020
THE ALLIES AFTER NASSAU IN DECEMBER 1960, THE U.S ........
*TEXT 020 01/04/63 PAGE 021
THE ROAD TO JAIL IS PAVED WITH NONOBJECTIVE ART SINCE THE KREMLIN'S SHARPEST BARBS THESE DAYS ARE AIMED AT MODERN ART AND WESTERN ESPIONAGE...
*TEXT 025 01/04/63 PAGE 024
RED CHINA FIXING FRONTIERS RED CHINA PRODUCED A SECOND SURPRISE LAST WEEK...
An this is my code, first the FileChooser and then the String.Split
import java.io.*;
import java.lang.Object.*;
import java.util.regex.*;
import javax.swing.JFileChooser;
public class Reader{
public static void main(String args[]) throws IOException{
File inFile;
FileReader fr;
BufferedReader bufReader;
JFileChooser chooser;
int reply;
String doc = "";
String line;
try{
chooser = new JFileChooser();
reply = chooser.showOpenDialog(null);
doc = chooser.getCurrentDirectory().getPath() + System.getProperty("file.separator") + chooser.getSelectedFile().getName();
inFile = new File(doc);
fr = new FileReader(inFile);
bufReader = new BufferedReader (fr);
do{
line = bufReader.readLine();
if(line ==null )
return;
} while(line!=null);
//**HERE STARTS THE STRING.SPLIT**
//"line" at the end of next line it supposed to be the whole .txt
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(line)));
StringBuilder br = new StringBuilder();
String newLine ="";
while(true){
if(line == null)
break;
br.append(line);
}
newLine = br.toString();
String arr[] = newLine.split("\\*TEXT");
System.out.println(java.util.Arrays.toString(arr));
//**HERE ENDS**
bufReader.close();
}//end try
catch(Exception e)
{ System.out.println("error: "+e.getMessage()); }
}//main
}//end class reader
Thanks for your help! :3
First and foremost, your code never reaches the call to split. Your code has only two paths: the user cancels the file chooser dialog, which causes a NullPointerException, or a file is chosen, which inevitably hits the return statement in
...
do{
line = bufReader.readLine();
if(line ==null )
return;
} while(line!=null);
Second, the line
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(line)));
tries to open the file with name specified by the value of variable line which is probably not what you want.
A fixed version of your code would be:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import javax.swing.JFileChooser;
public class Reader{
public static void main(String args[]) throws IOException{
File inFile;
FileReader fr;
BufferedReader bufReader;
JFileChooser chooser;
int reply;
try{
chooser = new JFileChooser();
reply = chooser.showOpenDialog(null);
// Read all the lines in the file at once
List<String> lines = Files.readAllLines(Paths.get(chooser.getSelectedFile().getAbsolutePath()), StandardCharsets.UTF_8);
// Merge the read lines into a String
StringBuilder sb = new StringBuilder();
for (String line : lines){
sb.append(line);
sb.append('\n');
}
String newLine = sb.toString();
// Split the String
String arr[] = newLine.split("\\*TEXT");
System.out.println(java.util.Arrays.toString(arr));
}//end try
catch(Exception e)
{ System.out.println("error: "+e.getMessage()); }
}//main
}//end class reader
Note the API classes Files and Paths are available starting Java API 1.7 only.
You can split the text using this regex:
^(?=\*TEXT)
Working demo
Related
Good Morning. Having trouble with a parser using split method. Goal is to read in txt file, extract should statements, then write a new txt file with those should statements. I have it working when the text is on one continuous line. If I have a new line in the txt file, rewrites the file with just the last line. Possibly the structure of my loops? Also any suggestions for saving new file from the directory in which it was opened? Thank you
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/*This Program Will launch a File Explorer.
User will then chose a .txt file to be parsed.
A new file will be created labeled "Parsed_(Document Name)".*/
public class Parser {
#SuppressWarnings("resource")
public static void main(String[] args) {
JFileChooser chooser = new JFileChooser();
Scanner userFile = new Scanner(System.in);
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName() + "\n");
File file = new File(chooser.getSelectedFile().getName());
String newFile = ("Parsed_" + file);
userFile = new Scanner(file);
while (userFile.hasNextLine()) {
String document = userFile.nextLine();
// Line breaks used by Parser
String[] sentences = document.split("\\.|\\?|\\!|\\r");
List<String> ShouldArray = new ArrayList<String>();
for (String shouldStatements : sentences) {
if (shouldStatements.contains("Should") || shouldStatements.contains("should"))
ShouldArray.add(shouldStatements);
}
FileWriter writer = new FileWriter(newFile);
BufferedWriter bw = new BufferedWriter(writer);
for (String shallStatements : ShouldArray) {
System.out.println(shallStatements);
bw.append(shallStatements);
bw.newLine();
}
System.out.println("\nParsed Document Created: " + newFile);
JOptionPane.showMessageDialog(null, "Parsed Document Created: " + newFile);
bw.close();
writer.close();
}
userFile.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
Test file 1 (works!)
Hello all. Here is a a packing list. You Should have a toothbrush. You should have a Phone charger. And you definitely should have your wallet!
Test file 1 output:
You Should have a toothbrush
You Should have a Phone charger
And you definitely should have your wallet
Test file 2 (Only printing last line)
Hello all. Here is a a packing list. You Should have a toothbrush. You Should have a Phone charger.
Here is some random text to show the parser will not include this.
You definitely should have your wallet!
test file 2 output:
You definitely should have your wallet
You need to create your result array outside of the loop
/** Placed here**/
List<String> ShouldArray = new ArrayList<String>();
while (userFile.hasNextLine()) {
String document = userFile.nextLine();
// Line breaks used by Parser
String[] sentences = document.split("\\.|\\?|\\!|\\r");
/** REMOVED HERE **/
for (String shouldStatements : sentences) {
if (shouldStatements.contains("Should") || shouldStatements.contains("should"))
ShouldArray.add(shouldStatements);
}
......
otherwise you will only gather the results of your last loop.
Basically what your code was doing:
cut up file in lines
take each line
take next line
make a result board.
write results on board
take next line
erase board
write results on board
take next line
erase board
write results on board
and then at the end there is only a limited resultset on your board
You are overriding your Arraylist within the loop, however you don't actually need it
File file = chooser.getSelectedFile();
System.out.println("You chose to open this file: " + file.getName() + "\n");
String newFile = "Parsed_" + file.getName();
// open all closable objects using try-with-resources
try (Scanner userFile = new Scanner(file);
BufferedWriter bw = new BufferedWriter(new FileWriter(newFile))) {
while (userFile.hasNextLine()) {
String document = userFile.nextLine();
// Line breaks used by Parser
String[] sentences = document.split("\\.|\\?|\\!|\\r");
for (String s : sentences) {
if (s.contains("Should") || s.contains("should")) {
System.out.println(s);
bw.append(s);
bw.newLine();
}
}
System.out.println("\nParsed Document Created: " + newFile);
JOptionPane.showMessageDialog(null, "Parsed Document Created: " + newFile);
// bw.close(); // not needed anymore
I've refactored the code, removing the "ShouldArray", which is not needed.
Pseudocode
While there are lines to read in the In file
Read each line
Split each line into Array of sentences
Loop through each sentence
If each sentence contains Should or should Then
Write sentence to Out file
End If
End Loop
End While
Close Out file
Close In file
The code below works with:
Multi line:
Hello all. Here is a a packing list.
You Should have a toothbrush. You Should have a Phone charger.
Here is some random text to show the parser will not include this.
You definitely should have your wallet!
Single line:
Hello all. Here is a a packing list. You Should have a toothbrush. You should have a Phone charger. And you definitely should have your wallet!
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File;
public class ShouldStringsParser {
public ShouldStringsParser(String inFile, String outFile) throws IOException {
File file = new File(inFile);
FileWriter writer = new FileWriter(outFile);
BufferedWriter bw = new BufferedWriter(writer);
Scanner userFile;
userFile = new Scanner(file);
String[] sentences;
while (userFile.hasNextLine()) {
String line = userFile.nextLine();
System.out.println(line);
sentences = line.split("\\.|\\?|\\!|\\r");
for (String shouldStatements : sentences) {
if (shouldStatements.contains("Should") || shouldStatements.contains("should")) {
System.out.println(">>>" + shouldStatements);
bw.append(shouldStatements);
bw.newLine();
}
}
}
bw.close();
writer.close();
userFile.close();
}
public static void main(String[] args) {
try {
new ShouldStringsParser("inDataMultiLine.txt", "outDataMultiLine.txt");
new ShouldStringsParser("inDataSingleLine.txt", "outDataSingleLine.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Read and write the text.
In read and write text file i have to print all the data in the text file but it print only the last line how to get all line write.
Program:
public class fs
{
try
{
BufferReader in = new BufferReader(FileReader(C:/Users/madhan kumar/Desktop/read.txt));
String s;
String[] result=null;
while((s=in.readLine())!=null)
{
result=s.split("\\|");
result = String[4];
String Name = result[0];
String age = result[1];
String Sex = result[2];
String field = result[3];
System.out.println("Name :"+Name+"Age :"+age+"Sex :"+Sex+"Field"+field);
BufferedWriter bw =new BufferedWriter (new FileWriter ("out.txt");
bw.write ("Name :"+Name+"Age :"+age+"Sex :"+Sex+"Field"+field);
Bw.close ();
}}
catch(Exception e)
{
System.out.println(e);
}
}
}
My txt file
malik|23|male|student
nakul|30|male|student
ram|27|male|worker
mak|25|male|student
The answer to your main question is that you only see the last line because you create a new BufferedWriter every time you write out to the .txt, and when you do that it deletes text already on the .txt file. To solve this problem simply declare your BufferedWriter outside of the while loop:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.StringTokenizer;
import java.io.FileReader;
import java.io.FileWriter;
public class fs{
public static void main(String[] args){
StringTokenizer str;
try
{
BufferedReader in = new BufferedReader(new FileReader("C:/Users/madhan kumar/Desktop/read.txt"));
BufferedWriter bw = new BufferedWriter (new FileWriter("out.txt"));
String s;
while((s=in.readLine())!=null){
str = new StringTokenizer(s, "|");
String name = str.nextToken();
String age = str.nextToken();
String sex = str.nextToken();
String field = str.nextToken();
System.out.println("Name: "+name+"\tAge: "+age+"\tSex: "+sex+"\tField: "+field);
bw.write("Name: "+name+"\tAge: "+age+"\tSex: "+sex+"\tField: "+field);
}
bw.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
I made a few small adjustments, largest being that I used StringTokenizer which does pretty much the same thing as your splitting method, but is a little more eloquent.
I have an input file like below. An input file contains list of 8 digit numbers separated by comma. For that we need to read input file mentioned below.
12345678,
12345679,
12345680,
12345681,
12345682,
12345683,
12345684,
12345685
as i have to read the above file. I need to insert first value after -d= sequence,second value at -d= sequence and so on...
finally output file must be:
D:\data\12345678.pdf -d=12345678
D:\data\12345679.pdf -d=12345679
D:\data\12345680.pdf -d=12345680
D:\data\12345681.pdf -d=12345681
D:\data\12345682.pdf -d=12345682
D:\data\12345683.pdf -d=12345683
here path and file name varies.but main goal is to insert those values after the -d= sequence of every line only.
How can i achieve this?
Thanks for any help
One way to accomplish this is
package com.test.file;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.MessageFormat;
public class OutputGenerator {
private static final String PATH = "D:\\data\\";
private static final String FORMAT = PATH + "{0}.pdf -d={0}";
public static void main(String[] args) throws IOException {
String inputFile = "/Users/chatar/Documents/dev/projects/weekend-practice/stack-overflow/src/com/test/file/input.txt";
String outputFile = "/Users/chatar/Documents/dev/projects/weekend-practice/stack-overflow/src/com/test/file/output.txt";
FileReader reader = new FileReader(new File(inputFile));
BufferedReader bufferedReader = new BufferedReader(reader);
FileWriter writer = new FileWriter(outputFile);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
try {
while(bufferedReader.readLine() != null) {
String inputLine = bufferedReader.readLine();
System.out.println("inputline :: "+ inputLine);
inputLine = inputLine.replaceAll("\\s+", "").replace(",", "");
MessageFormat format = new MessageFormat(FORMAT);
String outputLine = format.format(new Object[] {inputLine});
bufferedWriter.write(outputLine);
bufferedWriter.newLine();
}
}
finally {
bufferedReader.close();
bufferedWriter.close();
}
}
}
I'm coding in Java and I need to separate text that I get from a .txt into different parts of an Array. The text is composed by different "texts" like a collection of documents.
The line before each text is something like: "*TEXT" and some numbers, but I think, that with the only word "*TEXT" it can be divided each text.
An example of how is the .txt:
*TEXT 017 01/04/63 PAGE 020
THE ALLIES AFTER NASSAU IN DECEMBER 1960, THE U.S ........
*TEXT 020 01/04/63 PAGE 021
THE ROAD TO JAIL IS PAVED WITH NONOBJECTIVE ART SINCE THE KREMLIN'S SHARPEST BARBS THESE DAYS ARE AIMED AT MODERN ART AND WESTERN ESPIONAGE...
*TEXT 025 01/04/63 PAGE 024
RED CHINA FIXING FRONTIERS RED CHINA PRODUCED A SECOND SURPRISE LAST WEEK...
So I need the text 017 in a position of the array and in the next position will be the text 020.
How can I do this?
This is the code of how I get the text from the .txt using FileReader:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import javax.swing.JFileChooser;
public class Reader{
public static void main(String args[]){
File inFile;
FileReader fr;
BufferedReader bufReader;
JFileChooser chooser;
int reply;
String doc = "";
String line;
try{
chooser = new JFileChooser();
reply = chooser.showOpenDialog(null);
doc = chooser.getCurrentDirectory().getPath() + System.getProperty("file.separator") +
chooser.getSelectedFile().getName();
inFile = new File(doc);
fr = new FileReader(inFile);
bufReader = new BufferedReader (fr);
do{
line = bufReader.readLine();
if(line ==null )
return;
else{
System.out.println(line);
}
} while(line!=null);
bufReader.close();
}//end try
catch(Exception e)
{ System.out.println("error: "+e.getMessage()); }
}//main
}//end class reader
You could just read the entire file into a String and then use String.split(String regex)
You can use FileUtils to read the file and then you can just split it, like this
public static void main(String[] args) throws IOException {
for (String s:FileUtils.readFileToString(new File("/home/leoks/file.txt")).split("\n")){
if (s.startsWith("*TEXT")) {
System.out.println(s.split(" ")[1]);
}
}
}
or you can write a parser using something like this
http://txt2re.com/index-java.php3?s=*TEXT%20017&-14&-1
Sorry guys, disregard my answer. I typed it so i'm leaving it but I thought he just wanted the text number that was after the "*TEXT" identifiers.
Try regular expressions and captures.
String text = "this will be your document text"
Pattern p = Pattern.compile("(.*TEXT ([0-9]{3}))+.*");
Matcher m = p.matcher(line);
int numCounts = m.groupCount();
String texts[] = new String[numCounts];
for (int i = 1; i <= numCounts; i++) {
// group(0) is whole match you want each group a 1
texts[i-1] = m.group(i);
}
// now they should be in your texts
OR you can do this:
String text = "this will be your document text"
Pattern p = Pattern.compile("TEXT ([0-9]{3})");
Matcher m = p.matcher(line);
ArrayList<String> list = new ArrayList<String>();
while (m.find()) {
list.add(m.group(1));
}
String texts[] = list.toArray();
// now they should be in your texts
Try this way
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
new FileInputStream("yourTextFile")));
StringBuilder br = new StringBuilder();
String newLine ="";
while(true){
String line = bufferedReader.readLine();
if(line == null)
break;
br.append(line);
}
newLine = br.toString();
String arr[] = newLine.split("\\*TEXT");
System.out.println(java.util.Arrays.toString(arr));
I am working on a project for java and I want the comiler to differentiate between number and words, but when I try the code as is, it returns error due to string of -1. Also, how do I make the number I am reading in into * symbols in a graph? Any help would be greatly appreciated
Orville’s Acres, 114.8 43801
Hoffman’s Hills, 77.2 36229
Jiffy Quick Farm, 89.4 24812
Jolly Good Plantation, 183.2 104570
Organically Grown Inc., 45.5 14683
(What I am reading in)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.*;
import java.io.*;
public class Popcorn {
public static void main (String [] args) throws IOException {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getName();
Scanner infile = new Scanner(new FileReader( filename));
String line = "" ;
while (infile.hasNextLine())
{ line= infile.nextLine();
// int endingIndex =line.indexOf(',');
//String fromName = line.substring(0, endingIndex);
System.out.println(line);}
infile.close();
}
}
Use Integer.parseInt() to know if its a number. Have a catch exception block and if it comes in catch block, you'd know its not a number