This question already has answers here:
Advanced PDF parser for Java
(5 answers)
Closed 7 years ago.
I would like to ask, how can i parse text. I had extracted text from PDF file with PDFBox into normal text, which is output in console. For example this one:
SHA256: 51c11994540537b633cf91b276b3c34556695ed870a5d3f7451e993262a4a745
File name: ACleaner.zip
Detection ratio: 0 / 55
Analysis date: 20150721 12:23:19 UTC ( 8 minutes ago )
0 0
? Analysis ? File detail ? Additional information ? Comments 0 ? Votes
MD5 fffa183f43766ed39d411cb5f48dbc87
SHA1 b0d40fbc6c722d59031bb488455f89ba086eacd9
SHA256 51c11994540537b633cf91b276b3c34556695ed870a5d3f7451e993262a4a745
I need to get some values, for example value of MD5, File name etc..how can i reach it in Java? Thanks a lot
I have tried so : in this while a i added this
String keySHA256 = "SHA256:";
private static String SHA256Value = null;
if (line.contains(keySHA256)) {
// System.out.println(line);
int length = keySHA256.length();
SHA256Value = line.substring(length);
System.out.println("SHA256 >>>>" + SHA256Value);
}
but sometimes it doesnt get right value..please help..
This could be a good example for you to start learning more about Java IO and String parsing. Google is your friend.
//uri where your file is
String fileName = "c://lines.txt";
// read the file into a buffered reader
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) { //iterate on each line of the file
System.out.println(line); // print it if you want
String[] split=line.split(" "); // split your line into array of strings, each one is a separate word that has no spaces in it.
//add any checks or extra processes here
}
} catch (IOException e) {
e.printStackTrace();
}
Related
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Convert array of strings into a string in Java
(14 answers)
Closed last month.
I have a string input file and I put the strings into arrays. I only want to print the last element of each string array onto my console and onto a output array. When I run my code, I get the last element of each string array onto my console, but see this on my output file:
[Ljava.lang.String;#51d5f7fd
How can I print the actual string array to show up on output file and not its string representation? I'll show you my code so you get a better understanding of what I'm trying to do:
try {
br = new BufferedReader(new FileReader("C:\\rd\\bubble.txt"));
//first, create new file object
File file = new File("C:\\rd\\bubble_out.txt");
if(file.exists()) {
file.createNewFile();
}
PrintWriter pw = new PrintWriter(file);
String line = "";
while((line = br.readLine()) != null) { //while the line is not equal to null
String[] arr = line.split("\\s+"); //split at whitespace
System.out.println(arr[arr.length - 1]);
pw.println(arr);
pw.close();
}
}
catch(IOException x)
{
System.out.println("File not found");
}
}
}
}
}
I've tried using Array.toString() method, but I'm not sure how to implement it into my code correctly. I'm currently trying to do that, but if there's an easier way to do this, please let me know.
Use Arrays.toString to get a readable String representation of the array.
Don't close the PrintWriter inside the loop as you have not finished writing all output. You can use try with resources to avoid having to explicitly close it.
try (PrintWriter pw = new PrintWriter(file)) {
while((line = br.readLine()) != null) {
String[] arr = line.split("\\s+");
System.out.println(arr[arr.length - 1]);
pw.println(Arrays.toString(arr));
}
}
This question already has answers here:
How can I read comma separated values from a text file in Java?
(6 answers)
How do I split a string in Java?
(39 answers)
Closed 3 years ago.
I want to load data from a text file as required for part of a basic project. E.g. a text file can look like this:
201,double,70.00,2,own bathroom
202,single,50.00,2,own bathroom
Each piece of data is seperated by a comma, and in this case goes in the order: room number, room type, cost, amount of people, with/without bathroom and there's 5 data for each room and each room information is on a new line.
The code below reads each line individually, but how do I get it to read and store each data/word from the line (without the comma obviously)?
try{
BufferedReader reader = new BufferedReader(new FileReader("test.txt"));
String line = reader.readLine();
while (line != null){
System.out.println(line);
line = reader.readLine();
}
reader.close();
} catch(IOException ex){
System.out.println(ex.getMessage());
}
I saw an example using scanner but I heard that it's slower and less efficient.
I also tried using split but I can't figure out how to do it properly.
Thanks.
You can use Files.readAllLines() method and map the data to the dedicated object. Assuming you have such Room object with appropriate constructor you can read and store data like:
List<String> strings = Files.readAllLines(Paths.get("test.txt"));
List<Room> rooms = new ArrayList<>();
for (String line : strings) {
String[] split = line.split(",");
Integer roomNumber = Integer.valueOf(split[0]);
String roomType = split[1];
Double roomCost = Double.valueOf(split[2]);
Integer amount = Integer.valueOf(split[3]);
String bathroom = split[4];
Room r = new Room(roomNumber, roomType, roomCost, amount, bathroom);
rooms.add(r);
}
Then you can get the information for some room for example by room number:
Room room = rooms.stream()
.filter(r -> r.getRoomNumber() == 102)
.findFirst().orElseThrow(NoSuchElementException::new);
Note: If you are using java10 or above you can use orElseThrow() without parameters
You can split the line by the comma , and get an array of values:
try{
BufferedReader reader = new BufferedReader(new FileReader("test.txt"));
String line = reader.readLine();
String data[] = null;
while (line != null){
System.out.println(line);
line = reader.readLine();
data = line.split(","); //data will have the values as an array
}
reader.close();
} catch(IOException ex){
System.out.println(ex.getMessage());
}
If I´m not wrong then the described format is the csv format ( https://en.wikipedia.org/wiki/Comma-separated_values)
Here is good overview how you can read csv data:
https://www.baeldung.com/java-csv-file-array
This question already has answers here:
Reading UTF-8 - BOM marker
(9 answers)
Closed 5 years ago.
I applied this code more than one, this code is to read a file and for each line, it should create a new object and add it to att_agreement ArrayList, it works fine for each line except the first line, I can not find its object in the output.
Any help, please?
public ArrayList<Att_Elements> load_ann(File f) {
ArrayList<Att_Elements> att_agreement = new ArrayList<Att_Elements>();
String line="";
try {
BufferedReader read = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF8"));
while((line = read.readLine()) != null) {
String[] SplitLine = line.split("\\|");
if (SplitLine[0].equals("Att")) {
annotation=new Att_Elements();
annotation.Type = SplitLine[0];
.
.
.
//...
att_agreement.add(annotation);
}
}
read.close();
} catch (IOException e) {
e.printStackTrace();
}
return att_agreement;
}
Here is a sample of file content (3 lines):
Your file likely has what is called a BOM located at the beginning. This is a byte order mark. Thus, your conditional .equals("Att") is not being met until the second line where the BOM is not present. A separate if statement to handle this case should work well. If you print each line read, you should see what the BufferedReader is reading as the first line. The new conditional statement can then be tailored to this value.
Another approach is to search for a generic BOM string and replace it with nothing.
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 5 years ago.
I am trying to read text file with 3 lines:
10
PA/123#PV/573#Au/927#DT/948#HY/719#ZR/741#bT/467#LR/499#Xk/853#kD/976#
15.23#25.0#17.82#95.99#23.65#156.99#72.85#62.99#112.0#55.99#
So far in my main method I have:
`String fileName = "productData.txt";
String line = null;
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
}
catch(FileNotFoundException e) {
System.out.println(e);
}
catch(IOException e) {
e.printStackTrace();
}`
But Im not sure how I would go on with using the String DELIMITER = "#";
In the text file Line 1: is applied to number of types of product, Line 2: are product codes separated by #, and in Line 3: Price per unit of the corresponding products separated by #.
So Im looking for kind of format PA/123 Costs 15.23. How would I do that?
You can you line.split('#'); to get an array of Strings. This array contains your 10 elements.
Then you have two arrays of size 10. So firstArray[0] contains the name of the first product and secondArray[0] contains the price of it.
I'm trying to basically make a simple Test Generator. I want a button to parse a text file and add the records to my database. The questions and answers are in a text file. I have been searching the net for examples but I can't find one that matches my situation.
The text file has header information that I want to ignore up until the line that starts with "~ End of Syllabus". I want "~ End of Syllabus" to indicate the beginning of the questions. A couple of lines after that look for a line with a "(" in the seventh character position. I want that to indicate the Question Number line. The Question Number line is unique in that the "(" is in the seventh character position. I want to use that as an indicator to mark the start of a new question. In the Question Number line, the first three characters together "T1A" are the Question Group. The last part of the T1A*01* is the question number within that group.
So, as you can see I will also need to get the actual question text line and the answer lines as well. Also typically after the four Answer lines is the Question Terminator indicated by "~~". I don't know how I would be able to do this for all the questions in the text file. Do I keep adding them to an array String? How would I access this information from the file and add it to a database. This is very confusing for me and the way I feel I could learn how this works is by seeing an example that covers my situation. Here is a link to the text file I'm talking about:http://pastebin.com/3U3uwLHN
Code:
public static void main(String args[]) {
String endOfSyllabus = "~ End of Syllabus";
Path objPath = Paths.get("2014HamTechnician.txt");
String[] restOfTextFile = null;
if (Files.exists(objPath)){
File objFile = objPath.toFile();
try(BufferedReader in = new BufferedReader(
new FileReader(objFile))){
String line = in.readLine();
List<String> linesFile = new LinkedList<>();
while(line != null){
linesFile.add(line);
line = in.readLine();
}
System.out.println(linesFile);
}
catch(IOException e){
System.out.println(e);
}
}
else{
System.out.println(
objPath.toAbsolutePath() + " doesn't exist");
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new A19015_Form().setVisible(true);
}
});
}
Reading a text file in Java is straight forward (and there are sure to be other, more creative/efficient ways to do this):
try (BufferedReader reader = new BufferedReader(new FileReader(path))) { //try with resources needs JDK 7
int lineNum = 0;
String readLine;
while ((readLine = reader.readLine()) != null) { //read until end of stream
Skipping an arbitrary amount of lines can be accomplished like this:
if (lineNum == 0) {
lineNum++;
continue;
}
Your real problem is the text to split on. Had you been using CSV you could use String[] nextLine = readLine.split("\t"); to split each line into its respective cells based on tab separation. But your not, so you'll be stuck with reading each line, and than find something to split on.
It seems like you're in control of the text file format. If you are, go to an easier to consume format such as CSV, otherwise you're going to be designing a custom parser for your format.
A bonus to using CSV is it can mirror a database very effectivly. I.e. your CSV header column = database column.
As far as databases go, using JDBC is easy enough, just make sure you use prepared statements to insert your data to prevent against SQL injection:
public Connection connectToDatabase(){
String url = "jdbc:postgresql://url";
return DriverManager.getConnection(url);
}
Connection conn = connectToDatabase();
PreparedStatement pstInsert = conn.prepareStatement(cInsert);
pstInsert.setTimestamp(1, fromTS1);
pstInsert.setString(2, nextLine[1]);
pstInsert.execute();
pstInsert.close();
conn.close();
--Edit--
I didn't see your pastebin earlier on. It doesn't appear that you're in charge of the file format, so you're going to need to split on spaces ( each word ) and rely on regular expressions to determine if this is a question or not. Fortunately it seems the file is fairly consistent so you should be able to do this without too much problem.
--Edit 2--
As a possible solution you can try this untested code:
try{
BufferedReader reader = new BufferedReader(new FileReader("file.txt")); //try with resources needs JDK 7
boolean doRegex = false;
String readLine;
while ((readLine = reader.readLine()) != null) { //read until end of stream
if(readLine.startsWith("~~ End of Syllabus")){
doRegex = true;
continue; //immediately goto the next iteration
}
if(doRegex){
String[] line = readLine.split(" "); //split on spaces
if(line[0].matches("your regex here")){
//answer should be line[1]
//do logic with your answer here
}
}
}
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}