I would like to know if it's possible to add a line in a File with Java.
For example myFile :
1: line 1
2: line 2
3: line 3
4: line 4
I would like to add a line fox example in the third line so it would look like this
1: line 1
2: line 2
3: new line
4: line 3
5: line 4
I found out how to add text in an empty file or at the end of the file but i don't know how to do it in the middle of the text without erasing the line.
Is the another way than to cut the first file in 2 parts and then create a file add the first part the new line then the second part because that feels a bit extreme ?
Thank you
In Java 7+ you can use the Files and Path class as following:
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
lines.add(position, extraLine);
Files.write(path, lines, StandardCharsets.UTF_8);
To give an example:
Path path = Paths.get("C:\\Users\\foo\\Downloads\\test.txt");
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
int position = lines.size() / 2;
String extraLine = "This is an extraline";
lines.add(position, extraLine);
Files.write(path, lines, StandardCharsets.UTF_8);
You may read your file into an ArrayList, you can add elements in any position and manipulate all elements and its data, then you can write it again into file.
PD: you can not add a line directly to the file, you just can read and write/append data to it, you must manipulte de data in memory and then write it again.
let me know if this is useful for you
Related
I have a CSV file in my local folder. I would like to read it, remove a column, and replace the file in the same folder.
Actual sample data:
ID,EMAIL,FIRSTNAME,LASTNAME
99999,TestEmail#fakeemail.com,TEST_FNAME,TEST_LNAME
33333,TestEmail#fakeemail.com,TEST_FNAME,TEST_LNAME
Expected data from the sample data:
ID,EMAIL,FIRSTNAME
99999,TestEmail#fakeemail.com,TEST_FNAME
33333,TestEmail#fakeemail.com,TEST_FNAME
In this case, I would like to remove the column LASTNAME. Can it be done effectively in Java?
As in your example, you want to remove last column.
So easiest way would be to substring from beginning to last index of separator ,.
line.substring(0,line.lastIndexOf(","))
Full example:
try(PrintWriter pw = new PrintWriter(Files.newBufferedWriter(
Paths.get("path_to_output_file")))) {
Files.lines(Paths.get("path_to_input_file")).map(line -> line.substring(0,line.lastIndexOf(","))).forEach(pw::println);
}
If you need to remove other column, you may you split line.split(",") and than concatenate skipping column you want.
I'm trying to read a long file line by line and trying in the same time to extract some information from the line.
Here in an example of what i'm doing:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.stream.Stream;
public class ReadFile_Files_Lines {
public static void main(String[] pArgs) throws IOException {
String fileName = "c:\\temp\\sample-1GB.txt";
File file = new File(fileName);
try (Stream<String> linesStream = Files.lines(file.toPath())) {
linesStream.forEach(line -> {
System.out.println(line);
});
}
}
}
One line in my file is devided into three part :
10 1010101 15
I'm looking to read those three informations every time.
Like :
String str1 = line[0];
String str2 = line[1];
String str3 = line[2];
The solution i'm looking for will be better if i should not convert the stream to a collection.
I will use those 3 String to create a graph data structure, something like :
createGraphe(str1,str2,str3);`
I know that i can send the full String, but as i'm learning Stream I'm interested to know how to extract those informations.
Thank you.
You can map to an array by splitting each line, then call the method you want.
Files.lines(filePath)
.map(l -> l.split(" "))
.forEach(a -> createGraphe(a[0], a[1], a[2]));
The method lines() you are using already does what you expect it to do.
Java 8 has added a new method called lines() in Files class which can be used to read a file line by line in Java. The beauty of this method is that it reads all lines from a file as Stream of String, which is populated lazily as the stream is consumed. So, if you have a huge file and you only read first 100 lines then rest of the lines will not be loaded into memory, which results in better performance.
This is slightly different than Files.readAllLines() method (which reads all lines into a List) because this method reads the file lazily, only when a terminal operation is called on Stream e.g. forEach(), count() etc. By using count() method you can actually count a number of lines in files or number of empty lines by filtering empty lines.
Reference: https://javarevisited.blogspot.com/2015/07/3-ways-to-read-file-line-by-line-in.html
Since you want to solve this problem and want to learn how streams can be useful in this situation
Reading a file(Using Java8) , this will fetch you all the lines in the file: Stream lines = Files.lines(Paths.get(filePath))
Reading this file line by line : lines.map(line -> line.split(pattern)) , by splitting the line and you will get three sections from the line
Passing the arguments obtained into the function : forEach(arg -> createGraphe(arg[0], arg[1], arg[2]);
I hope this is pretty elaborate for your answer if you want to achieve this
It is known that the dash (-) in a YAML file before the key value pair is necessary to show that it is separate block(it is what I think). Figure 1 shows the YAML I'm generating using YamlBeans jar.
field1:
- childfield1:
datafield1:
param1:
childparam: paramvalue
param2:
childparam2: paramvalue
param3:
childparam3: paramvalue
datafield2: value2
For my codebase can't be changed, I have to somehow create the YAMLs as shown in Figure 2 (a tab is appended in each line in yaml file) or remove the dash is removed. You can clearly observe that there are only two thin vertical lines in Figure 1 but three thin vertical lines in Figure 2 which shows the alignment of the blocks.
What I want to achieve is to remove that dash from the first block (at the child field) from the file. Using a YAML file reader and writer always introduces the dash.
Glancing quick at (but admittedly not being familiar with) YamlBeans, it doesn't look like it's easy to subclass the behavior of the Emitter. One option though is to generate a temporary form in memory, then manipulate the results when writing out to a file. For example
// let YamlWriter write its contents to an in-memory buffer
StringWriter temp = new StringWriter();
YamlWriter yamlOut = new YamlWriter(temp);
yamlOut.write(someObject);
// then dump the in-memory buffer out to a file, manipulating lines that
// start with a dash
PrintWriter out = new PrintWriter(new FileWriter(new File("someoutput.dat")));
LineNumberReader in = new LineNumberReader(new StringReader(temp.toString()));
String line;
while ((line = in.readLine()) != null) {
if (line.startsWith("-")) {
line = line.substring(1);
}
out.println(line);
}
my specifics may be off, but hopefully the approach of doing simple manipulations of a temporary copy is clear enough.
If I were personally doing this, I'd probably write a custom subclass of java.io.Writer and do the manipulation on the fly (but i haven't gone through YamlWriter/Emitter in enough detail to provide an example on how to do that)
EDIT: I guess this thread can be closed, since all my questions have been answered! Thanks to everyone who helped me!
EDIT: I stumbled upon an error at openFileInput("myfilename.txt");. This is the error: The method openFileInput(String) is undefined for the type Game. I read an answer here: Android File I/O openFileInput() undefined, but must admit that I don't quite understand it...
I'm trying to read parts of a text file, till the token ";". This is the code I wrote:
InputStream instream = openFileInput("myfilename.txt");
String line=null;
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
while((line=buffreader.readLine())!=null){
String[] parts=line.split(";");
int intOne = Integer.parseInt(parts([0]);
int intTwo = Integer.parseInt(parts([1]);
String strLine = parts([3]);
}
public static void Start(){
while(there is text in the file){
// read first line till ';';
// name that variable intOne;
// read first line after ';' till next ';';
// name that variable intTwo;
// read next line, declare as strLine;
// System.out.println(strLine);
}
}
Beneath it is the idea of what it should do. But I have some questions:
Am I right to say that the String[] parts is an array?
I want to have a bigger file, but read only 3 lines per loop. Or could I, when I have a file of 100 lines, read that all at once, then recall them from the parts[]? Or would that take way too much time?
Where should the text file be? When I'm testing in Eclipse, in the project folder? And when I export it inside the jar?
Hope someone can answer my questions!
(My source is: Read specific string from each line of text file using BufferedReader in java, all credits to Gilbert Le Blanc!)
EDIT: When I do this in the file:
Hello,
I am coding;
Will the pars[0] be Hello,, because that's one line, or Hello, I am coding? And will it take the enter with it?
Another EDIT:
I wish to create some sort of textbased RPG engine, where you only have to edit the text file, to change the story. For example (in the text file):
30;60; //Score needed for this piece of the story
Hello!; // The text
Hi!;5; // The first possible answer; the score you'll get when you answer this
Shut up!;-5; // The second possible answer; the score you'll get when you answer this
What you rly want is reading one char after another. I used some nice BufferedSegmentReader from the framework Smooks, which could be interesting for you. Look at the sourcecode here:
http://grepcode.com/file/repo1.maven.org/maven2/org.milyn/milyn-smooks-all/1.5/org/milyn/edisax/BufferedSegmentReader.java
It reads characters one after another from a stream and puts it into a StringBuffer. There are delimiters to indicate when one "segment" is done reading, and after that you can work with this segment till you tell the BufferedSegmentReader to move on.
I think this rly suits your case and is an approach you are looking for.
This question already has answers here:
How to remove line breaks from a file in Java?
(17 answers)
Closed 9 years ago.
I am writing in a file and I want it to output into a csv file this way:
Group Members
1 Name1
Name2
Name3
code:
FileWriter fw = new FileWriter(csv);
BufferedWriter pw = new BufferedWriter(fw);
pw.write("Group,");
pw.write("Members");
pw.newline();
String mem=some_method();
pw.write("1,");
pw.write(mem);
pw.newLine();
My String mem has '\n' every after the names and so when it writes in the file, '\n' is read then writes into the next line. How can i achieve the above output without modifying the some_method()?
You want to strip the new line characters in mem.
Check out
Remove end of line characters from Java string
Put simply you can add the line mem = mem.replaceAll("(\\r|\\n)", "");
Try as below,
String mem=some_method();
mem = mem.replace("\n", "");
Instead of \n you can also use System.getProperty("line.separator") which gives you platform independent line separator.
mem = mem.replace(System.getProperty("line.separator"), "");
Why dont you combine the Group and Member together in one write method like:
pw.write("Group \t\t Members");
and do the same with the other contents. Hope this helps.