FAQ Database Discussion Community


Java Read Text file with condition

java,split,bufferedreader
I'm just wondering if there's a way to read a text file and skip a line with specific string. For example (test1.txt): test0,orig,valid,nice test1,input,of,ol,[www] test2,[eee],oa,oq test3,wa,eee,string,int test4,asfd,eee,[tsddas],wwww Expected output : test0,orig,valid,nice test3,wa,eee,string,int I already have this code : String line; String test[]; try{ LineIterator it = FileUtils.lineIterator(file2,"UTF-8"); while(it.hasNext()){ line =...

How do I structure text file?

java,bufferedreader
I have a little program: String userHomeFolder = System.getProperty("user.home"); String userDesktop = userHomeFolder + "/Desktop"; File file = new File(userDesktop, "test.txt"); try { Scanner sc = new Scanner(file, "UTF-8"); BufferedReader br = new BufferedReader(new FileReader(file)); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } sc.close(); }...

Line when reading a file is empty but the line is not null

java,bufferedreader,java-io
I have a problem in java and i dont understand why, since i think i am doing text-book stuff. An overview in what of want to do is: I want to create a file that contains in each line two strings: documentPath, documentID (in this format: "documentPath;documentID;") I want to...

Should BufferedReaders be passed a dynamic FileReader?

java,performance,bufferedreader,filereader,java-io
As the question states, is passing a dynamic FileReader bad practice? Should this/can this FileReader be closed sufficiently? BufferedReader buffReader = new BufferedReader( new FileReader(new File("C:/Path/To/File.txt"))); .... do something buffReader.close(); Should the FileReader also be closed? Would separation be required to do so? Perhaps a simple question, but I'm not...

Why is my BufferedReader not “Ready”?

java,java.util.scanner,bufferedreader
I looked up all of the BuffedReader "not ready" questions even slightly related to what I was looking for but I could not find the solution to my problem. I have a Client and a Server sending/recieving String using the appropriate methods (i.e. BufferedReader, PrintWriter), but my BufferedReader will not...

Using Java bufferedreader to get html from URL

java,html,url,character-encoding,bufferedreader
I'm trying to read all the html from a page using a buffered reader like follows String charset = "UTF-8"; URLConnection connection = new URL(url).openConnection(); connection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); connection.setRequestProperty("Accept-Charset", charset); InputStream response = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(response,charset)); then I'm reading it line...

Read from file and write it to another file

java,algorithm,file,bufferedreader
I need to write a file in java based on reading multiple file templates. File template 1: 010 Date 131231 131231 131231 131231 131231 131231 020 NAME 131231 131231 131231 131231 131231 131231 030 YEAR 131231 131231 131231 131231 131231 131231 090 xxx 131231 131231 131231 131231 131231 131231 File...

Android BufferedReader does not read the whole response

java,android,url,bufferedreader
I have a problem with reading a ULR response. On Android it only reads around the half of the response. If I use the same code in a normal Java project everything works fine. try { String _output = null; URL url = new URL("http://example.com"); BufferedReader buffer = new BufferedReader(new...

Confusion about String's replaceAll method

java,parsing,bufferedreader,bufferedwriter,replaceall
I'm attempting the will essentially take an input file and write out an output file that has each word and punctuation of the input on a separate line. Example input: System.out.println("hey there"); Example output: System.out.println ( "hey there" ) ; here is my code: import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException;...

Android URL connection java.io.FileNotFoundException

java,android,email,httprequest,bufferedreader
I try to send a normal Http Request to a WordlPress Website. This is the URL: "http://localhost.com/api/submit_comment/?post_id=12345&name=Max&[email protected]&content=hallo" If I open this with my Web-Browser all works fine. If I open it over this over an Android App it throws a java.io.FileNotFoundException. The Android Code: class SendComment extends AsyncTask<String, Void, String>...

Reading .ini File When Converted into Runnable Jar File

java,bufferedreader,filereader,ini,ini4j
I have a project which I am going to convert it into runnable jar file. I am storing some of the configurations in my ini file with using ini4j. It works when I simply set the directory getConf = new Ini(new FileReader(path));, but it wont work when I use getResourceAsStream()...

Accents aren't print after UTF encoding/decoding

java,unicode,encoding,inputstream,bufferedreader
I've read several articles on the whole topic and I still don't understand what's going on here. Please, see for yourselves in the following working example (actually, no example, this is the complete class I'm working on with some added main()). public class Console extends JFrame { private static final...

Why is this method returning an ArrayList with all the same objects? JAVA

java,file,csv,arraylist,bufferedreader
I have a class called User and a file called Users.csv, like below: User class: public class User { private String name; private String rg; private String type; public String getName() { return name; } public String getRg() { return rg; } public String getType() { return type; } public...

Java - open txt file and clear all multiple spaces

java,bufferedreader,bufferedwriter
I have a txt file and what I am trying to do is open it and delete all multiple spaces so they become only one. I use: br = new BufferedReader(new FileReader("C:\\Users\\Chris\\Desktop\\file_two.txt")); bw = new BufferedWriter(new FileWriter("C:\\Users\\Chris\\Desktop\\file_two.txt")); while ((current_line = br.readLine()) != null) { //System.out.println("Here."); current_line = current_line.replaceAll("\\s+", " ");...

Reading a large text file faster

java,bufferedreader
I'm trying to read a large text file as fast as possible. Lines not beginning with '!' are passed over. Lines with 8 CSV have their last value removed. There will never be a ',' in a value (didn't need to use opencsv). Everything is added to a long string...

Java BufferedReader.readLine() returning null when reading file

java,nullpointerexception,null,bufferedreader
I need some help. This is my function: public String[] getLines(String filename) { String[] returnVal = null; int i = 0; try { BufferedReader br = new BufferedReader(new FileReader(new File(filename))); for(String line; (line = br.readLine()) != null; ) { // process the line. returnVal[i] = line; i++; } br.close(); }...

Understanding how BufferedReader works in Java

java,bufferedreader,filereader
Very basic question on how BufferedReader works. Given the string/phrase, i want to find and print it from the file with a lot of text in it. using BufferedReader in Java I did some research on this topic and that was the closest result. Not quite adressing my problem though....

Buffered Writer and Reader Proper order/use

java,logic,bufferedreader,bufferedwriter
So I'm not sure where my mistake is in the order/use of BufferedWriter and BufferedReader. See for yourself. My program works all except for these methods. Here is where I declared stuff, pretty sure error is not here. fw = new FileWriter(file, true); bw = new BufferedWriter(fw); fr = new...

BufferedWriter can't desire change data

java,arrays,file,bufferedreader,bufferedwriter
i hope you can help me in this, I am reading a file with multiple lines. I use BufferedReader to separate the datas. If data 1 does not match with new data 1 input, it will use BufferedWriter to replace the new data 1 input into old data 1. With...

Java wrapper for ROOT. OutputStreamReader is blocking

java,bufferedreader,root-framework
TLDR; is there a friendly way to get Java to read from ROOT stdout? And vice versa? I have a Java function that launches a ROOT process. Java and ROOT then communicate via stdin and stdout. Well, that's the plan anyway. For some reason I can't get any info output...

Reading N lines from a file in java?

java,arrays,file,java.util.scanner,bufferedreader
I am not quite sure how to explain my question but I will try my best. Say for example, I have a file containing 100 numbers, is it possible to read lines 25-50 from this 100 numbers file. To read N amount from begining, I would do something like this;...

Why we need to use BufferedReader instead of String while getting the response from server

java,android,httpclient,bufferedreader
I am doing an android application, Which is getting some JSON values from server. So I made some studies and develop the code for get data from server to my app. Simply I am using the below code for that. HttpResponse response; Object content = null; HttpGet httpget = new...

Java - Writing and Reading ArrayList to and from .txt file - BufferedWriter

java,bufferedreader,filereader,filewriter,bufferedwriter
Some Background: I'm making an RPG text based game for class, and I'm using an ArrayList for the inventory, since of course, it will constantly be changing whenever another item is added. I want to be able to save this ArrayList into a txt file and I want to be...

What does this condition do (BufferredReader and InputStreamReader)?

java,bufferedreader,inputstreamreader
I'm currently reading a code and i found a method starting like this : BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { } Can someone explain me the condition while ? That a = b != c seems weird to me....

Java Accessing Hexidecimal Text Files For HashMaping

java,iterator,hashmap,hex,bufferedreader
I am making a GUI were I have to make buttons and the background changes. The program is using a Java Collection Framework were I went with a Hashmap. I am really having trouble understanding and trying to do is access a txt file then read the pairs and store...

Data stored in different lines on a file, how to load them as parameters to a class?

java,initialization,bufferedreader,readline
I'm trying to do a small sentence translator, each sentence has one or more translations that will be loaded from a file formatted like this: EN – english sentence 1 IT – italian translation 1 EN – english sentence 2 IT – italian translation 1 IT – italian translation 2...

stdin Java Input

java,stdin,bufferedreader
I am having problems with STDIN I would to read the following 2 string, for example: Input: abc xyz When typing "abc", then press Enter, I get abc back. However i dont want that. I would like to type another string just like input above. So what want is: Type...

How can i pass all integers i this loop and stop before the last line

java,linked-list,bufferedreader,stringtokenizer,parseint
this is my input first line 5 6 3 4 2 3 2 5 1 0 word 2 2 4 i need to add all the integers to a graph but not the last line (word 1 2 4).. i have splitted the first line (first line etc.) and put...

BufferReader populating 2D array missing last line

java,multidimensional-array,bufferedreader
I'm trying to use BufferReader to populate a 2D array and it successfully populates all of the rows of the array except one, the very last one. I've tried increasing the loop by one to account for that line, but I get an index out of bounds error. How can...

NullPointerException caused by Reading from File used for indexing using Google Guava [duplicate]

java,indexing,guava,bufferedreader,multimap
This question already has an answer here: What is a Null Pointer Exception, and how do I fix it? 12 answers I am working with Google Guava to use a Multimap like this for example {S1.E11=[S2.E236], S1.E108=[S2.E371]} . But I have a problem. I want to read from file...

Java: BufferedReader Keeps Writing Values 128-159 as 63 When Converting to Char

java,bufferedreader,filereader,filewriter,bufferedwriter
I am trying to write a hex editor. I'm trying to store values by writing a char to a text file. For some reason every decimal number 128-159 is being written or read (not sure which) as 63. I took measures to isolate the problem. Here's an example of it...

How do I read next line with BufferedReader in Java?

java,bufferedreader,readline
I have a text file. I want to read it line by line and turn it into an 2-dimensional array. I have written something as follows: BufferedReader br = new BufferedReader (new FileReader ("num.txt")); String line = br.readLine(); while( line != null) { System.out.printf(line); } This turns into an infinite...

How to change a file reader to a directory scanner?

java,loops,directory,bufferedreader,inputstreamreader
Answer System.out.print("Enter the name of the directory:"); File a= new File((new BufferedReader(new InputStreamReader(System.in))) .readLine()); File[] b=a.listFiles(); for(int i=1;i < b.length;i++){ fn=b[i].getPath(); } Problem System.out.print("Enter the name of file: "); fn = (new BufferedReader(new InputStreamReader(System.in))) .readLine(); I have tried File[] file Array=new File(System.in).listFiles(); for(File f: fileArray) // loop through all files...

How would I go about searching for a string of words across multiple lines

java,parsing,input,io,bufferedreader
This is a homework assignment I am working on which required me to read data from a file and the parse through it finding the line number and starting index of certain key phrases or names. The file is being hosted online so I've decided to use the URL class...

java hadoop: FileReader VS InputStreamReader

java,hadoop,bufferedreader,filereader,inputstreamreader
i want use my java class on the hadoop hdfs, now i must rewrite my functions. the problem is, if i use the InputStreamReader my app read wrong values. here my code (so it's work, i want use the uncommented code part): public static GeoTimeDataCenter[] readCentersArrayFromFile(int iteration) { Properties pro...

How do I create a 2 dimensional array from a text file in Android?

android,multidimensional-array,bufferedreader
How can make a two dimensional Array of integers by reading from a .txt file that looks like this: 0000 0100 1233 Would you use BufferedReader or InputStream? Here is what I have so far and it either crashes, or it just says 52, 52, 52.... public static void loadTileMap(String...

How to store strings of text files for later use in Java

java,bufferedreader,jfilechooser
Please forgive me, I'm relatively new to Java. Basically I'm wanting to get some string for a text file for later use after selecting the file through a filechooser. This is a snippet of the code that I have written so far. public void actionPerformed(ActionEvent e){ if(e.getSource() == openButton){ returnVal...

File Reader Method

java,logic,bufferedreader,filereader
So I wrote this file reader method that should return a string of everything that is in the file, but it isn't working properly. Writing into the file works perfectly, but this reading method doesn't. What the method does currently is it reads the last string/text added, but it does...

Chaining InputStream in Java

java,inputstream,bufferedreader
So i know that InputStream sees the data as raw data and Reader sees it as characters. Buffered them will make things more efficient. But how about chaining them like this: InputStream in = new InputStream(...); in = new BufferedInputStream(in); Reader r = new InputStreamReader(in); r = new BufferedReader(r); Is...

Get page content from URL in java

java,url,bufferedreader
Not able to access content of this page "kissanime.com" (it's not returning anything) from URL by this code : String a="http://kissanime.com"; url = new URL(a); URLConnection conn = url.openConnection(); try ( // open the stream and put it into BufferedReader BufferedReader br = new BufferedReader( new InputStreamReader(conn.getInputStream()))) { String inputLine;...

Assigning instances of objects to an array

java,arrays,bufferedreader,java-io
I am using bufferedFileReader and lineScanner to read through a csv file, delimiting at commas and assigning the first token in the line to an object of class Team. Each token after this is assigned to a variable of Team. I have this part working fine. The next part is...

Java read from one file and write into another file using methods

java,file-io,bufferedreader,bufferedwriter
I am learning Java and working on File IO and right now stuck in reading text from One file and write in another file. I am using two different methods first one for reading and displaying text in console from file #1 and using another method to write in file#2....

Read all lines with BufferedReader

java,bufferedreader,infinite-loop,inputstreamreader
I want to type a multiple line text into the console using a BufferedReader and when I hit "Enter" to find the sum of the length of the whole text. The problem is that it seems I'm getting into an infinite loop and when I press "Enter" the program does...

Java BufferedReader - empty line issue

java,file,javafx,line,bufferedreader
Problem description: For example we have a text file file.txt with following content: Hello(LS) what(LS) are(LS) <empty_line>(LS) you(LS) doing(LS) <empty_line>(LS) <empty_line>(LS) now(LS) <empty_line>(LS) <empty_line>(LS) <empty_line>(EOF) (LS - line separator, EOF - end of file) If I understood the idea of text file, the file is looking something like that. Now,...

Improve Data Transfer through socket from desktop to android service

java,android,sockets,bufferedreader,data-transfer
I'm kind of new to Socket Programming, I searched the web a lot and it just confusing me more. So I want to transfer data from a desktop app which I wrote in Java, to my Android device. Here's the logic: I start a server socket from the app in...

Common way to use System.in several times without closing

java,bufferedreader,system.in
I have a console menu that has to read inputs a few times in different methods. I use a new BufferedReader(new InputStreamReader(System.in)) to do that. But if this reader is closed in a method it's not useable/openable again because of System.in. To solve this problem one possibility is to static...

readLine of BufferedReader does not change file pointer even if buffer size is small

java,android,bufferedreader,randomaccessfile
My app reads text file line by line and record offset of each line until the end of file. offset changes only when readLine is first executed. It does not change any more after that. I tested with bufferSize from 10 to 16384. What is wrong with my code? I...

writing and reading a txt content using java

java,io,bufferedreader
I want to read data from a text file and columns are seperated by a space like this 1 BNA 5000 10.81 1 CER 1000 20.33 1 ADV 500 1.91 1 MNT 20000 5.53 and I want to write data----------------- to a text file in the same format,, and it'd...

Trying to insert text from file to 2D array. Java. Text is different formats

java,arrays,bufferedreader
So I have a text file containing ints, doubles, strings that I need to read into a 2D array. there Array should have 6 columns but the rows are not known until you read the entire file. I'm guessing it is some 700 rows This is what I have so...

Reading from a file Russian characters(javaSE)

java,regex,bufferedreader
Sorry for my English. Why, if I want to find English words, everything is no problem. But if I want to find a Russian word, it does not output anything? try(BufferedReader fileOut = new BufferedReader(new FileReader(file))){ for(String line; (line = fileOut.readLine()) != null; ){ if(line.contains(commandString)) System.out.println(count + ": " +...

How to read a large file of Strings in chunks, each time from different offset, in Java?

java,file,memory,bufferedreader
I have a large file that consists of several lines, where each line is actually one word of English. The complete file does not fit in memory. As such, I want to process it chunk by chunk. So, I need to implement something like this: Method1 : Read one chunk,...

BufferedReader space separated input

java,input,bufferedreader
first I'd like to mention that I am not realy experienced in java, and I searched StackOverFlow for a solution to my problem and either I didn't find it or didn't understand the answer, so I am asking now: i wanted to start working with BufferedReader and didn't find any...

Why is eclipse complaining when I try to close BufferedReader in finally block?

java,eclipse,exception,bufferedreader,try-catch-finally
Here is my code: public static String readFile() { BufferedReader br = null; String line; String dump=""; try { br = new BufferedReader(new FileReader("dbDumpTest.txt")); } catch (FileNotFoundException fnfex) { System.out.println(fnfex.getMessage()); System.exit(0); } try { while( (line = br.readLine()) != null) { dump += line + "\r\n"; } } catch (IOException...

Implementing Iterator interface with exception handling

java,unit-testing,iterator,bufferedreader,factory-pattern
I need to create a class library which enables me to read different files (.dat-files with different data representations inside them) and create objects with their content (for every line one object). I also have to create a unit test which starts the reading of the file, so I dont...

Trying to read a data.txt file and do calculations

java,bufferedreader,filereader
I am using Integer.parseInt() to change the String variables of each line of data.txt to an int data type. The data.txt file looks like this: 5 6 3 5 0 ...and so on. I also have no spaces in the text file so I should be able to parse the...

Java reading a string from a text(txt) file

java,string,bufferedreader,filereader
Rod Rae Bryan Shiroe Ric Kirito Asuna Elsa Akutabe Shino I have that list saved in a text file. If I were to enter Rod, it should say "Exists" and if I enter a name that is not on the list, it should say "Does not exist." But what...

BufferedReader found specific word in line, take the data from that line

java,arrays,file,bufferedreader
br = new BufferedReader(new FileReader(file)); while ((finalLine = br.readLine()) != null) { String[] reader = finalLine.split(","); String foundWord = "dod"; for (String word : reader) { if (word.equals(foundWord)) { String NAME = reader[0]; String PASSWORD = reader[1]; String CONTACT_NO = reader[2]; String EMAIL_ADDRESS = reader[3]; I'm currently using BufferedReader...

File reading/writing proper order

java,file,bufferedreader,bufferedwriter
So I'm working on this program that needs to create a file, writes into that file, and then read from that file. However, the file is not being written into but is still being created. First, Here are my declarations from the constructor in my file doing stuff class (called...

Reading java file with escape characters for newline

java,escaping,bufferedreader,unicode-escapes
I have a Unicode file that needs to be exported to database(Vertica). The column delimiter is CTRL+B, record delimiter is newline(\n). Whenever there is a newline within a column value, CTRL+A is used as escape character. When I use BufferedReader.readLine() to read this file, the records with ID's 2 and...

Buffered Reader read text until character

java,file,while-loop,bufferedreader,readline
I am using a buffered reader to read in a file filled with lines of information. Some of the longer lines of text extend to be more than one line so the buffered views them as a new line. Each line ends with ';' symbol. So I was wondering if...

How to use BufferedReader correctly with sockets

java,android,sockets,chat,bufferedreader
The application is a basic chat client. I got a Thread for getting data from the server. I want to get every response from the server separately. It prints in the console only when the loop breaks (when i send "exit" using the other parts of the application). So when...

Array of Buffered Readers in JAVA

java,arrays,bufferedreader
Is it possible to have an array of Buffered Readers like BufferedReader[] b = new BufferedReader(5); and Initialise them individually like b[0] = new BufferedReader(new FileReader("foo")); ...

Issue with BufferedReader.read while reading from InputStream

inputstream,bufferedreader
I am trying to read input stream in chunks and write to file to avoid memory issue, I am receiving data in json format and same I write to file using following code. BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); BufferedWriter writer = null; if(getOutputFormat().equals(Constants.URI_FORMAT_JSON)){ boolean enableAppend = true; CharBuffer buffer...

Android http request readline

java,android,httprequest,bufferedreader,readline
I was trying to readline through http request of the source code of the web page. My purpose was to read an integer value (price) from the result. I'm using bufferedReader to readline. I had no clear idea on which method could store the data I wanted. Below is the...

Skip a line using BufferedReader (skip, but not read it)

java,file,file-io,bufferedreader
Hi guys I am currently using BufferedReader to read files. I have something like: br.readLine() != null for each call loop. And now what should I do if I want to skip a line. Here, I've read several similar questions other people posted, most of them suggested using readLine(). I...

How to store text from a certain column in an arraylist?

java,file,bufferedreader
I would like to store only the first column that is contained in the .txt file. hello28 23232 hello27 23232 hello25 12321 This is the code I have so far, however at the moment it stores every line in the file; how can I make it so that only the...

BufferedReader seems to infinitely read the first line

java,process,bufferedreader,handbrake
I'm trying to run Handbrake through a Java app I'm writing, and am having trouble waiting for Handbrake to finish. When I try this : ProcessBuilder builder = new ProcessBuilder( "cmd.exe", "/c", command); Process p = builder.start(); BufferedReader inputreader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while((line = inputreader.readLine())...

Splitting a text file into multiple files by specific character sequence

java,text,split,bufferedreader,stringbuilder
I have a file with the following format. .I 1 .T experimental investigation of the aerodynamics of a wing in a slipstream . 1989 .A brenckman,m. .B experimental investigation of the aerodynamics of a wing in a slipstream . .I 2 .T simple shear flow past a flat plate in...

Java extract text from text file from a certain point on the text

java,filter,inputstream,bufferedreader,inputstreamreader
I have created a method with BufferedReader that opens a text file created previously by the program and extracts some characters. My problem is that it extracts the whole line and I want to extract only after a specified character, the :. Here is my try/catch block: try { InputStream...

How can I read a text file, search for commas, treat commas as new lines, and export it to a new file using Java?

java,text,bufferedreader,filereader
I have a .txt file with 1 billion items separated by commas. I want to be able to read the file.txt file, allow my script to read the commas, copy the item before the comma into a new file, and start a new line after every comma. Example of the...