line 3 « Development « Java I/O Q&A





1. how to remove duplicate lines from a file    java-forums.org

public static void main(String[] args) { try{ FileInputStream fstream = new FileInputStream("D:/Files/Sample.txt"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; LinkedHashSet set = new LinkedHashSet(); //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console set.add(strLine); // System.out.println (strLine); } System.out.println("Set :" + set); //Close the input ...

2. Reading a specific line from a file    java-forums.org

Hi! I know how to read a line from a txt file by using a buffered reader String line = ""; String tempLine = ""; BufferedReader bufferedReader = new BufferedReader(new FileReader(filename)); // use the readLine method of the BufferedReader to read one line at a time. line = bufferedReader.readLine(); But I dont know how to read a specific line in a ...

4. How to make a program that replaces each line of a file with its reverse    java-forums.org

Hello everyone! Peace be with you! I am trying to make a program that replaces each line of a file with its reverse. The way I want to do it will involve reading and writing a text file (IO). Of course the file will not be being written and read at the same time. I want to do it in a ...

5. Java- Writing a file and reading a file line by line    java-forums.org

1.What is the default place/location ( where the .txt file written is saved) for file write in java, using class FileWriter and BufferedWriter. Is it saved in the package in which lies the class which contains the code for file write? 2.Can we save the .txt file created after file writing using java class FileWriter and BufferedWriter? 3.When we want to ...

6. I want to know how many lines are in a File I open    java-forums.org

Do a loop? While its not null, set this variable to this variable+1. When it is null, set this variable to another variable and do what else you wanted to do? While readLine != null increment counter if its null, set linesInFileVarto increment counter. Set increment counter to 0 Do whatever else you planned on doing afterwards.

7. Trying to write to a file but it only prints last line    java-forums.org

import java.io.*; public class NsaEncoder { public static void main(String[] argv) throws Exception { BufferedReader fin; BufferedReader cin; cin = new BufferedReader(new InputStreamReader(System.in)); // open a file whose name is entered by the user String fileName; System.out.println("What file do you want to use for input? "); fileName = cin.readLine(); fin = new BufferedReader(new FileReader(fileName)); while (true) { if (!fin.ready()) break; String ...

8. Printing to the screen specific lines from a file    java-forums.org

/** * * @author steven.haynes */ import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class LogSearch { public static void main(String[] args) throws FileNotFoundException { Scanner file = new Scanner(new File("file.txt")); int startLineNo = 0; int endLineNo = 0; String line = ""; while (file.hasNext()) { line = file.nextLine(); startLineNo++; if (line.equals(args[0])) { System.out.println("Argument exists at line: " + startLineNo); break; } ...

9. Reading file method stops on blank line - It should not    java-forums.org

Java Code: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class fileScan { public static String validationMessage = ""; /** * @param args the command line arguments */ public static void scanTheFile(String fileTitle) { File file = new File("C:\\SyncDB\\test.txt); FileInputStream fin; ...





10. I need to truncate a file with 10 or more lines, to 10, removing the first lines    java-forums.org

Hello guys, I have a file.txt with 10 or more lines, data gets appended continuously but i want to maintain 10 lines in this text file at every time. I am looking at achieving this by checking the total lines immediately after inserting, if >10 then delete from line 1 to line (total-10) I have seen loads of ways to do ...

11. How to read from a file line by line,token by token    java-forums.org

Read an entire line (it's a String) and split on one or more spaces "\\s+"; you get back an array of Strings, not containing spaces; when the array has three elements the original line represented a node/vertex; when the array contains two elements, the line represented an edge. The Integer.parseInt( ... ) method can transform the individual Strings to ints. kind ...

12. Reading Next Line of File    java-forums.org

13. search third word of all lines in a file    forums.oracle.com

14. read second line from file    forums.oracle.com

String s1 = line2.substring(8, 18); double n1 = Double.parseDouble(s1); //X String s2 = line2.substring(21, 30); double n2 = Double.parseDouble(s2); //Y String s3 = line2.substring(32, 42); double n3 = Double.parseDouble(s3); //Z double u = ((n2*w2) + (n3*w3)) / ((Math.sqrt(Math.pow(n2, 2) + Math.pow(n3, 2)))*(Math.sqrt(Math.pow(w2, 2) + Math.pow(w3, 2)))); double A = Math.acos(u); double Y = n2*Math.cos (A) - n3*Math.cos(A); double Z = n2*Math.sin ...

15. How to write new line to a file using fileoutput code in java    forums.oracle.com

aString end My problem is i get a space in between astring and end it is not a TAB i believe?????????? What shall i do??????????? If i use: try { File f = new File("C:\\WINDOWS\\system32\\drivers\\etc hosts"); RandomAccessFile raf = new RandomAccessFile(f, "rw"); // Read a character char ch = raf.readChar(); // Seek to end of file raf.seek(f.length());

16. Easy way to write mutliple lines to a file?    forums.oracle.com

Yes because the ideal is to call jtextfiled or jtextarea .getText() which only returns a single string with all new lines characters delimited? Do this change things? But what about if it doesn't come from the getText() method call but I just decide to pass it one string with a bunch of new line characters? Are these two situations different? Thanks ...





17. Read the file line by line from upload    forums.oracle.com

Hi Guys, Need help... I creating a web application that has a upload capability. What I want to do is this... I want to upload a file (but not save it). What I'm going to do is just read the file line by line and save these data to the database. How can I do this? (BTW, I'm using struts (FormFile)). ...

18. How do i print to a new line to a file?    forums.oracle.com

19. Unable to catch lines from html file    forums.oracle.com

20. print new line in files    forums.oracle.com

21. can't read more than one line out of file...    forums.oracle.com

22. Skipping lines when reading a file    forums.oracle.com

23. how to get the numbers of line of a file?    forums.oracle.com

24. How to read the particular line in a file !!!    forums.oracle.com

i want to read the 5th , 8th or some other line number of the file. This file is consists of many data arranged by line number. I'm able to retrieve the LineNumber of the file but i'm not able to point the curser to that particular line so it can read that line only. i'm pointing the cursor to that ...

25. Writing to file line by line    forums.oracle.com

Try reading the file into a string array, as a space to the end of each string and rewrite the file. No need for a new file, just overwrite, although that may not be the best policy if the program ends during rewrite, your's is probably the best way to do it.

26. how to write to a secific line in a file    forums.oracle.com

27. Delete a line from a file    forums.oracle.com

Hi all, I need to delete a line from a text file after finding the relevant word in it. Is there any class which allows this in an efficient way. The way I found is to read the file fully, delete it from memory, delete the file then write it back. Any other way ?. If this is the only way ...

28. printing lines to files    forums.oracle.com

import java.io.*; import java.util.*; public class ListingsWriter { public ListingsWriter(String name, double bid, String timeleft) { createFile(name, bid, timeleft); } public void createFile(String name, double bid, String timeleft) { name = name; bid = bid; timeleft = timeleft; String dirfile = "C:/Files/" + name + ".txt"; System.out.println("dirfile set to " + dirfile); try{ FileWriter wrt = new FileWriter(dirfile); PrintWriter pw = ...

29. copying one file to another loses a few lines    forums.oracle.com

31. Stop writing to a file when number of lines exceed    forums.oracle.com

Hi, I want to stop writing to a file when I have written 50 lines in that file. How can I bring that check in my program? I know that linenumberreader can be used to read a file and check the line..but how can I do this when I am writing to a file? Thanks

32. Adding/Writing new lines inside a file (between specific line)    forums.oracle.com

Note that in general you won't want to have the whole file in memory at once. The more common approach is to read a line or group of lines at a time and see if they're the lines you want, and write them back out. If they were the lines you're looking for, you then also write out whatever additional lines ...

33. Remove last character from every line in a file.    forums.oracle.com

Hi ! Thanks for reply ! However I am talking about scenario where there will be more than 1 million line in file. I don't want to waste my time doing it manually using some editor. Like in Unix we have sed , is there any code in java which can do it.

35. How to get a random line from a file?    forums.oracle.com

Hi, I have a file. I want to get a line from it randomly and print it. I have seen some posts here but they are not completely answered or explained. I can read the lines using bufferedreader but this reads line by line and not a specific line. Please help. Thanks

36. Count the number of lines in each file in a directory    forums.oracle.com

Assuming the directory only contains "text" files (how many "lines" are in an executable file, or in a .xls, .doc, etc file? Undefined), then you'd have to read each file and count the lines as you read them. There's really no such thing as a "line". It's all up to the software to determine what a "line" is. Normally apps such ...

37. how to read a specific line of a .dat file?    forums.oracle.com

can we read a specific line of a .dat file, if so, how? i have this .dat files for each customer, which contain their name on the first line, their id on the second line and a list of items and an item id between each item. John Doe 455689 milk 1 orange 9 Here is what I am trying to ...

39. read line by line from a file    forums.oracle.com

40. Number of lines being written into a file    forums.oracle.com

Hi All, I get "n" number of input text files - having "m" number of lines each. Based on the first character of each line (there are 5 distinct types I can get) i have to write each line in a separate file. To reduce I/O i am first storing the lines in "m" different StringBuffer objects (based on the first ...

41. How to delete and edit particular line in a file and save it in same file ?    forums.oracle.com

Hi, I want to delete and edit text at particular line in a file. But edit and delete should reflect in same file. I have done googling for this but it results with using another file, that i dont want as i need to save changes in same file. How can i do this? Thanks in advance Edited by: vj_victor on ...

42. Adding line to beginning of existing file    forums.oracle.com

I want to write a program that appends one line to the beginning of an exsisting text file. The problem is that I don't want the original file to be erased. Well, it is the normal way of doing those things, i.e. 1) open a new file for writing 2) write the new first line 3) open the old file for ...

43. Determining number of lines in a File without loading it to memory    forums.oracle.com

I'm just wondering if there is a way to know the number of lines in a file without having to load the file entirely into memory and count the lines. in my project i have a restriction that says that i'm not allowed to load into memory more than 10 KB. i have to read the file block by block, say ...

44. reading the second line of a file    forums.oracle.com

46. read to get the last line from a File    forums.oracle.com

Hi I am sending in a clarification , hoping that I get an efficient solution from the group, should somebody have come across this scenario and implemented. How to read or get the last line from a text file , which usually have a no of records, without actually reading or looping through all the lines I know there is some ...

47. Increment and Decrement the line of a file    forums.oracle.com

if ((lineFromFile.charAt(i) == '@')) { char s = lineFromFile.charAt(i); while(true) { s--; // this is currently changing the '@' to a '?' instead of moving to what should be the char right before the @ in the line of the file. if (s < 0) break; if (isValidEmailCharacter(lineFromFile.charAt(s)) == false) break; // a call to a method i have in the ...

48. How to read the nth line from a specific file in a better way?    forums.oracle.com

I am calculating the total number of lines (step 1) using the same logic of BufferedReader. The code is working as expected. But as I need to keep displaying that random line in an infinite while loop and the file contains ~ 4000 lines, it's creating performance issues. Is there any better way to read a particular line of a file ...

49. Delete last line of a file    forums.oracle.com

Both your problems can be solved as follows: - read all lines in a collection; - remove a specific line; - write the remaining lines back to the same file (overwriting the original). If your file is large and thus cannot read it entirely in memory, do: - read the file line by line (using some buffered-reader); - write all the ...

50. How do i read a file line by line backwards?    forums.oracle.com

This looks like a CSV file. If it is, and you want to have a robust solution that will correctly parse all the different CSV scenarios properly (like taking into account commas inside quotes as you have above, also noting commas inside quotes inside quotes, etc...) then you should work with a pre-build CSV parser. If you search google you will ...

51. How to Read the last line of the file    forums.oracle.com

My initial reaction is to try reading the whole file and time this -- it may be fast enough. If it isn't, use RandomAccessFile to read, say, the last 1000 bytes of the file and work out how much of this is the last line. It helps if you are using a simple encoding like Latin-1.

52. reading an input file line by line.    forums.oracle.com

53. How to get the number of lines of a file?    forums.oracle.com

54. Reading the same line twice from a file    forums.oracle.com

I have code which I have to read the same line of a file when I want. Reading the file by line line to an arraylist or a variable is not the thing that I want. I just want to mark a line position in a file and read to start from the position. Random Files uses seek to do this, ...

55. Disabling blank line at the end of output file    forums.oracle.com

Hi there, I have a method that uses the FileWriter, and PrintWriter streams to output text to a file using the println() method. It however always leaves a blank line at the end of the file, I know that in Linux this is necessary, but I am using a Windows based system and this is an annoyance that I find on ...

56. Reading specific lines in a file    forums.oracle.com

hI i need to write a program which has a file name a.txt the contents in the file is SELECT DISTINCT D.MENUNAME, D.MENULABEL, D.MENUSEP, D.MENUORDER FROM PSMENUDEFN D, PSAUTHITEM A, PSOPRCLS C WHERE D.INSTALLED = 1 AND *D.MENUGROUP = *:1** AND D.MENUNAME = A.MENUNAME AND A.AUTHORIZEDACTIONS > 0 AND A.CLASSID = C.OPRCLASS AND *C.OPRID = *:2** ORDER BY D.MENUORDER, D.MENUNAME Bind-1 ...

58. Writing in a file line by line    forums.oracle.com

FileInputStream fstream = new FileInputStream("great.txt"); File f1=new File("greater.txt"); FileOutputStream fop=new FileOutputStream(f1); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; while ((strLine = br.readLine())!=null) { fop.write(result.getBytes()); } fop.flush(); fop.close(); } } The output I am getting is Hi Hello..the Hello is not getting printed in next line...please suggest me wat to do

60. change value in file without touching other lines    forums.oracle.com

hi gurus, I have a property file, i have almost 40-50 keys/values with comments and blank lines in it. i want to change the value of couple keys using the program If I use, properties.store() method, it changes removes all the comments, blank lines and the order using stringbuffer and reading, changing and writing again to diff file is long using ...

61. how to add 3 lines(of integer data)at any position of a file(size increase)    forums.oracle.com

i have the below file....which contains 1 green apples 2 red apples 3 oranges 4 bananas 5 grapes BEGIN_DATA 1 5 2 3 2 3 5 4 2 4 5 1 4 2 2 4 1 END_DATA //end of file after BEGIN_DATA, each line indicates a 'transaction'. After every 2 transactions i have to add 3 more transactions which are generated ...

62. how to read line by line a file in reverse order    forums.oracle.com

Thanks for your suggestion. I've tried using RandomAccessFile but when I call readLine() method it returns me single character not complete line. And if I tried to implement as suggested you in option 2, if my file contains more than 20000 lines then this will be inefficient way to solve the problem and might be possible out of memory error might ...

64. append a String in file (line by line)    forums.oracle.com

65. Writing data to New Line in a File    forums.oracle.com

66. File reading line by line    forums.oracle.com

67. Adding a line to a file that already exists    forums.oracle.com

69. Search and Delete a line from a file    forums.oracle.com

Hello everyone, I am a newbie in Java. I have just started working with Java, and I am mightily stuck with a code. I have to develop a function which has to search for a string in a file and then delete the lines which contain that string. The string is only a small part of the whole line and isn't ...

71. How to read a particular line in a file    forums.oracle.com

72. when i print more than one line to a file, it overwrites the previous lines    forums.oracle.com

do { PrintWriter pw1=new PrintWriter(new BufferedWriter(new FileWriter("questions.txt"))); PrintWriter pw2=new PrintWriter(new BufferedWriter(new FileWriter("options.txt"))); PrintWriter pw3=new PrintWriter(new BufferedWriter(new FileWriter("correctanswers.txt"))); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the question."); String read=br.readLine(); pw1.write(read); System.out.println("Enter the corresponding options."); read=br.readLine(); pw2.write(read); System.out.println("Enter the correct answer."); read=br.readLine(); pw3.write(read); System.out.println("Continue??(0-yes,1-no)"); int opt=Integer.parseInt(br.readLine()); if(opt==1) { pw1.close(); pw2.close(); pw3.close(); break; } }while(true); thats my code. when i open questions or ne other file ...

73. read from file two lines    forums.oracle.com

74. fastest way to read file line by line    forums.oracle.com

Dear all, At the moment I have a sample data below.. I need to read this from text file (100MB) and put into CSV format. In data below once we meet the word "END" means a block of data. And i need to pick up some records from every line of data. For example IMSI line i will get 456010700000028 in ...

75. skip empty lines in a file    forums.oracle.com

76. Method to Update One Line in File?    forums.oracle.com

I've searched and cannot seem to find the answer. I have a configuration file for an application. I want to change one line in the file. dosomething=1 I then want to change to dosomething=0 From what I can see, it appears the only way to do this is to load the whole file into memory and then write the whole file ...

77. New lines in a file    forums.oracle.com

Thread.sleep(100); 19:31:36 Sleep... 19:31:41 Sleep... 19:31:47 Sleep... 19:31:52 Sleep... 19:31:57 Sleep... 19:32:02 Sleep... 19:32:07 Sleep... 19:32:11 Sleep... That could be one reason/cause. its sleeping every ~5 seconds, and I clearly say 0.1, so during the other 4.9 its stuck in the loop? Wtf? I've changed it to: while ((sb.append(reader.readLine())).toString() != null && !sb.toString().equals("")) { onNewLine(sb.toString()); sb.delete(0, sb.length()); }

79. Reading files for a specific line    forums.oracle.com

So first things: let's say I have a file called calendar.txt. This is what it currently contains: 042607,I have to go to work today. The first part is the date and the second part is my entry separated by a comma. When I click a button, I want it to search for a specific line. In this case, the user entered ...

81. Entering lines when writing to a file    forums.oracle.com

(using notepad) notepad expects the sequence "\r\n" to indicate a new line. Didn't see that parenthetic remark - sorry, for being a bit cryptic. newLine() will output a line separator defined by the system property line.separator. This is the right thing if you want to use the file with software that expects this line separator, like Notepad. But if you are ...

82. Writing in Files at the Line (END-1)    forums.oracle.com

I need to make my program write something not at the end of the program, but just before the last line. The basic idea is to have the word END at the end of the file, but when writing new things, I can either delete the things written before (which is not an option) or write at the end of the ...

83. reading file lines and tokenization.    forums.oracle.com

I will try with line.split("[ , ] "); From your answer, you almost understood the question. I needed to parse the line numbers so that I can later call it and say that in Line 1, I have 1A, 1C, 1F and 1G. In line 2, 1B, 1H, 1K. Like you rightly said, I will end up having two A's, ...

84. Number of lines in a file    forums.oracle.com

because I don't want to read the last line until way after the rest, and because the file will change size I need to know the number of lines so that I can stop the program reading the last line. Edit: oh and thanks for the quick replies Message was edited by: manutdfan

85. read blocks of lines from file    forums.oracle.com

Hi, I am new to java. I have a large file it contains the lines. Each line will be terminated with \n. I do not want to read all lines at once, I would like to read some blocks of lines at once. I really did not find a method to read specified number of lines. I found there some methods ...

86. how to write to a file with \n (new line)???    forums.oracle.com

87. how can i get the number of lines of a file?    forums.oracle.com

88. How can I Read lines of data from a file starting at the end of the file??    forums.oracle.com

Can anyone help me with how to read lines of data starting from the end of a file instead of the beginning?? I do not want to load the entire file into memory as the files are very rather large. Instead I want to start at the end of the file and read lines backward , until I find the particular ...

89. How to get rid of empty lines in a file?    forums.oracle.com

It really isn't very sensible to try and do this kind of editing on a text file "in place". If the file is a sensible length you could simply read the whole thing into memory, remove the blank lines, and overwrite the original file. You may need to give some thought to what happens to the file if the program doesn't ...

90. Wanna skip exact number lines while reading a file file    forums.oracle.com

Hi all, I would like to skip exact number of lines while reading a text file. Let's say I wanna read this text file starting from line no N. And so I need to skip from fist line to N-1th line. Does anyone give me a way to do? Pls with a sample code if possible coz I am not familiar ...

91. why the lines are not kept when write it to the file?????    forums.oracle.com

i have the above code to remove the spaces between sentences i.e i have a file with sentences and the spaces between each sentences and anther some time one line or more so i had that code to make all the spaces between the sentences unquie i did use the println and the printWriter still the buffer has no spaces and ...

92. Counting Words, Line, and Char in a file    forums.oracle.com

Basically, the java.io package has two main ways of dealing with I/O: by bytes or by characters. InputStreams provide an input consisting of a byte sequence, OutputStreams provide byte output, Readers provide character-based input, and Writers provide character-based output. There are a bunch of subclasses of these that provide special functionality. For example, BufferedReader is a Reader that provides buffering. InputStreamReader ...

93. Reading a file line by line and executing the code    forums.oracle.com

If you're hoping to execute lines of java source code on the fly...then forget it. Java is a compiled language. It doesn't have an "eval" statement like other languages. Now, you can write an interpreter for a language in Java. And I suppose you could write an interpreter for Java in Java (although at that point, you might as well choose ...

94. Updating one line of a file    forums.oracle.com

You can use RandomAccessFile, but if you do so make certain to pad out each line with as many spaces or nulls as you'll need room for. If you put in "John:99" then a newline and then "Andy:12", and then you increment John's number, you're going to screw up the next entry. Personally I think it's a mistake to do it ...

95. Parsing a file - each line of file has integers, flooats,doubles , strings    forums.oracle.com

I am trying to parse a file. Each line of the file has ints, doubles, floats and strings. I am using BufferedReader class. I cant use StringTokenizer class here. I want to get each tokens and pass them into my method columnname.setValue(object, row, col) of DefaultTableModel class. Which tokenizer class would be suitable in this situation. Please help me.

96. Reading lines beyon null lines in a file    forums.oracle.com

While writing a file-processing script, I came across the following case where I need to read info like ENDDOC and PGCount for multiple records that are in the same file. ; Record 1 @C ENDDOC# ATYC-12769314 @C PGCount 20 @T ATYC-12769295 ; Record 2 @C ENDDOC# ATYC-12769317 @C PGCount 3 @T ATYC-12769315 Can you suggest how should I read the lines ...

97. read certain line from file    forums.oracle.com

If the file does not have fixed-length lines then getting to the line you want is not so easy if it is based on the number of lines in the file (as 3rd from the bottom would be). You may need to read the file backwards using RandomAccessFile (sounds like a pain and maybe inefficient) or keep a buffer of previously ...

98. counting lines in a text/csv file.    forums.oracle.com

Can you advice me, how I can get the total number of lines in a text/csv file please using java code. I will get the text/csv file content in a string variable not as a file. EX: string var = "123\n234\n123\n3456\nsdfsd\n" here \n is in the new line. for this I have to get the total lines as 5. Please advice. ...

99. how to update last line in a file.    forums.oracle.com

Personally I have never needed to use RandomAccessFile. Why are you using it? Do you have a performance problem? I may be wrong, but I think that RandomAccessFile was more useful back in the 80's & 90's. The way HDs are constructed now and the way they are written to seek time is much more of a factor than writing throughput ...