I have just relised that I have a file where only one line exists with a long string. This file (line) can be300MB heavy.
I would like stream some data from this string and save in another file
i.e the line from the file would look like:
String line = "{{[Metadata{"this, is my first, string"}]},{[Metadata{"this, is my second, string"}]},...,{[Metadata{"this, is my 5846 string"}]}}"
Now I would like to take 100 items from this string from one "Metadata" to another "Metadata", save it in the file and continue with the rest of the data.
So in the nutshell from one line I would like to get N files with i.e. 100 Metadata strings each
BufferedReader reader = new BufferedReader(new StringReader(line));
This is what I've got and I don't know what I can do with the reader.
Probably
reader.read(????)
but I don't know what to put inside :(
Can you please help
Related
I have text file in a local storage with some data. Now i need to update data for only selected line data.
This is my textfile data
Kumar
Gaaa
9394769487
Male
Un Married
Tue Jun 12 00:00:00 IST 1984
kumar#gmail.com
Kumar#1993
rtyhrty
India
here i am receiving new data from different fields.So i need to update
particular lines of data to related data only
ex: Here first line is Name=kumar,
i want to update Name=ajay
this is my reading logic and put all the values into HashMap object.
can you tell me any another process is there for putting all lines of data to particular key in a Map object.
String temp;
String[] line=new String[15];
InputStream stream=new FileInputStream(path);
BufferedReader reader =new BufferedReader(new InputStreamReader(stream));
while((temp = reader.readLine()) != null)
{
for(int j=0; j<line.length ;j++)
{
line[j]=reader.readLine();
String fname=line[0];
String lname=line[1];
String mobile=line[2];
String gender=line[3];
String marry=line[4];
String dob=line[5];
String email=line[6];
String pwd=line[7];
String address=line[8];
String country=line[9];
String idcard=line[10];
String idfile=line[11];
String addcard=line[12];
String addfile=line[13];
map.put("fname",fname);
map.put("lname",lname);
map.put("mobile",mobile);
map.put("gender",gender);
map.put("marry",marry);
//map.put("dob",dob);
map.put("email",email);
map.put("address",address);
map.put("country",country);
map.put("idcard",idcard);
map.put("idfile",idfile);
map.put("addcard",addcard);
map.put("addfile",addfile);
}
}
stream.close();
reader.close();
You can use map or read line by line both are efficient based on below scenario
If reading the file one time and editing the file as and when requirement comes is what you need then storing the data in static map(class level static variable) is your best solution.By doing this you don't need to read the file again and again whenever the modification for file comes change the value in map and overwrite the whole map data to file
If you want to read the file each time there is a modification request then the best thing to do is to read it line by line and modify the line for which the request came and then overwrite the file.It does not make sense to store the file data in map each time if you are going to read the file at every request.
Hope This Helps !!
I would suggest reading the file line by line, change what lines you need to change and write all lines to a new file, then delete the old file and rename new file. Unless the file is very large and you only need to change first few lines.
Read the file into a map
Alter the map
Write the map into the file
I have a text file with various key value pairs separated with a '--'.
Below is the code I have so far
File file = new File("C:\\StateTestFile.txt");
BufferedWriter out = new BufferedWriter(file.getAbsolutePath());
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
if(line.contains("content_style")) {
//Write to the line currently reading and save back to the file
}
}
br.close();
out.close();
What I would like to do is read this text file and replace the value of a specific line with something I specify. So id want to find the 'content_style' line and replace 'posh' with 'dirty'.
How can I do this?
simply use:
line = line.replaceAll("posh", "dirty"); // as strings are immutable in java
This can be done in-place on a single file only if you are sure that the string you are replacing is exactly the same length in bytes as the string that replaces it. Otherwise, you can't add or delete characters in a single file, but you can create a new file.
Open the source file for reading.
Open the destination file for writing.
Read each line in the source file, use replaceAll, and write it to the destination file.
Close both files.
Alternate method that preserves the key-value semantics:
Open the source file for reading.
Open the destination file for writing.
Split each line in the source file into a key and value pair. If the key equals "content_style", write the key and "dirty" to the destination file. Otherwise write the key and value.
Close both files.
Finally, delete the old file and rename the new file on top of the old one. If you're going to be doing key-value manipulations often, and you don't want to write out a new file all the time, it might be worth it to use a database. Look for a JDBC driver for SQLite.
I want to specifically overwrite data in a file starting from a given line.
Suppose that I find out that I have to write some data in the file from line x ( I have already found x) . How would I overwrite everything after there.
Also is there a function that would directly take my line and string and overwrite the file.
An alternate approach would be, read the file line by line by scanner class (as described below), store those lines into any variable, say, arraylist, then appennd your new string once you have read the lines and write the whole list into a new file.
Example:
File file = new File("file.txt");
Scanner scanner = new Scanner(file).useDelimiter("\n");
String line = scanner.next();
//Store in the list
//Append the new lines
//Write the whole list into a new file
I want to read the last 2 lines in some files, and if the content of second last line matches a specific string, then delete the last line only.
Also, after the above operation, 2 lines of data have to be appended to the end of the modified file. I saw other questions on SO that deal with different parts of my problem, but is there an easy way of doing all of the above with minimal code, preferably in a single function? (I can combine the different functions available at SO but that would be messy...)
I would recommend you to do it "in memory". It's easy to read line by line into a List, check the last lines and update the lines and write it back to the file.
Example:
public static void main(String[] args) throws IOException {
String fileName = "test.txt";
List<String> lines = new ArrayList<String>();
// read the file into lines
BufferedReader r = new BufferedReader(new FileReader(fileName));
String in;
while ((in = r.readLine()) != null)
lines.add(in);
r.close();
// check your condition
String secondFromBottom = lines.get(lines.size() - 2);
if (secondFromBottom.matches("Hello World!")) {
lines.remove(lines.size() - 1);
lines.add("My fixed string");
}
// write it back
PrintWriter w = new PrintWriter(new FileWriter(fileName));
for (String line : lines)
w.println(line);
w.close();
}
Note: No exception handling is done in the example above... you need to handle cases where the file for example doesn't contain two lines and other problems!
If you have really big files and perfomance is an issue the way to go is to use a RandomAccessFile and read backwards looking for the line termination bytes to determine where the last two lines begin. Otherwise use dacwe's approach.
As Gandalf said you can:
take RandomAccessFile,
use method seek(long) to jump forward and read those lines. But you won't know exactly how big the jump should be.
to delete last lines you need the position of begin of last line so before reading each line store their file pointer position (method getFilePointer()). Deleting to that position you use setLength(long).
My example of reading and deleting last lines you have here:
Deleting the last line of a file with Java
Useful can be also:
Quickly read the last line of a text file?
I have a requirement that I need to append block of a file to other file.
let say I have 100 lines in source file and I need to append the 50 lines from the bottom to destination file.
To navigate to the 50th line I using .readLine() of the BufferedReader.
Once I reached to 50 th line I want to append remaining content to the destination file.
I don't want to append line by line as it is consuming much time.
Please help me how to do that..
Please provide a code snippet if possible.
read the file line by line and append in StringBuffer object
for eg
StringBuffer lineBuffer = new StringBuffer();
String line = "";
while((line=br.readLine())!=null){
///// any condition or anything you want to do
lineBuffer.append(line);
}
now after you get the lines into the stringbuffer object write this into another file