473,595 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

replacing a specific part of a specific line of text inside a file

blazedaces
284 Contributor
Alright guys, so the title explains exactly my goal. The truth is I'm going to be reading in a lot of data from an xml file. The file is too large and there's too much data to store in arraylists without running out of memory, so I'm reading and as I'm reading I'm going to write to a file.

This is the thing though, I already can do this and have it done, but I want to modify the program so you can choose what data you want to take out. To do this I would set up the text file to show something like this:

Expand|Select|Wrap|Line Numbers
  1. one:1,2,3,4,5,
  2. two:1,2,3,4,5,
  3. three:1,2,3,4,5,
  4. four:1,2,3,4,5,
  5. five:1,2,3,4,5,
  6.  
Where one, two, etc. are data names and the numbers are data separated by commas as you see.

Here's my problem. I will not run into all the data at one time. Every second is another piece of information, so when I get to that element in the xml I want to open the file, locate the correct data (what line), then replace that line, so it could look like this afterwards:

Expand|Select|Wrap|Line Numbers
  1. one:1,2,3,4,5,
  2. two:1,2,3,4,5,
  3. three:1,2,3,4,5,
  4. four:1,2,3,4,5,6,
  5. five:1,2,3,4,5,
  6.  
Now, I wrote something using RandomAccessFil e in java, here's all my code (it's a test program):

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class testwriteMoreToFile {
  5.     private RandomAccessFile raf;
  6.     private static String fileToBe;
  7.  
  8.     public void writeMoreToFile(String tagName, String newData) {
  9.         try {
  10.             try { 
  11.                 raf = new RandomAccessFile( new File(fileToBe), "rw");
  12.             } catch (FileNotFoundException e) {
  13.                 raf = new RandomAccessFile(fileToBe, "rw");
  14.             } 
  15.  
  16.             StringBuffer contents = new StringBuffer();
  17.             String line = null;
  18.             long prevFilePointer = 0;
  19.  
  20.             while ((line = raf.readLine()) != null) {
  21.                 if (line.substring(0,tagName.length()).equals(tagName)) {
  22.                     contents.append(line).append(newData).append(",").append(System.getProperty("line.separator"));
  23.                     raf.seek(prevFilePointer);
  24.                     raf.writeChars(contents.toString());
  25.                     break;
  26.                 }
  27.                 prevFilePointer = raf.getFilePointer();
  28.             }
  29.         } catch (IOException e2) { 
  30.             e2.printStackTrace();
  31.         } finally {
  32.             try {
  33.                 raf.close();
  34.             } catch (IOException e2) { 
  35.                 e2.printStackTrace();
  36.             }
  37.         }
  38.     }
  39.  
  40.     public static void main(String args[]) {
  41.         String[] tagNames = new String[]{ "one","two","three","four","five" };
  42.         String line = new String("1,2,3,4,5,");
  43.         String[] lines = new String[5];
  44.         fileToBe = "Z:\\test.txt";
  45.         PrintWriter out = null;
  46.         for(int i = 0; i < lines.length; i++) {
  47.             lines[i] = tagNames[i]+":"+line;
  48.         }
  49.         try {
  50.             out = new PrintWriter(fileToBe);
  51.             for (int i = 0; i < lines.length; i++) {
  52.                 out.println(lines[i]);
  53.             }
  54.         } catch (IOException e2) { 
  55.             e2.printStackTrace();
  56.         } finally {
  57.             out.close();
  58.         }
  59.  
  60.         testwriteMoreToFile test = new testwriteMoreToFile();
  61.  
  62.         test.writeMoreToFile(tagNames[3], "6");
  63.     }
  64. }
  65.  
I predicted this problem before I tried it, but thought maybe it could work so I'll try it anyway.

The output looks like this in the text file:

Expand|Select|Wrap|Line Numbers
  1. one:1,2,3,4,5,
  2. two:1,2,3,4,5,
  3. three:1,2,3,4,5,
  4.  f o u r : 1 , 2 , 3 , 4 , 5 , 6 , 
  5.  
  6.  
  7.  
After I add the line (and it doesn't even seem to add correctly, why the spaces?) bytes after that probably don't read correctly..

Any ideas guys, just ideas, what do you think? Any better concepts?

I've got this one that I read somewhere:

Two files:

Copy first line of file 1 to file 2
repeat until you get to line you want to change
change the line, put it in file 2
continue till end copying to file 2

Problem is if I do this sooooo many times because of all the data I"ll need to do it with will this take a very long time? Even so, what, 300 copies of a file? I should delete the old one right, I believe something was mentioned about a kill method... Can I delete the old one and then change the new file name to be the old file name?

Thanks for all the help guys...

-blazed
Jul 6 '07 #1
10 15029
Finomosec
7 New Member
First, there are many different XML-parsers.
- Those which load the complete file into memory (bad for big files)
- Those which walk the XML-file from top to bottom (Visitor-Pattern i think)
maybe SAX-parser, if i'm not mistaken

Maybe using another XML-parser solves your problem in the first place.

Second i would do something like this:
Expand|Select|Wrap|Line Numbers
  1. public class FileTest {
  2.  
  3.     public static void main(String[] args) throws Exception {
  4.         BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
  5.         try {
  6.             BufferedWriter writer = new BufferedWriter(new FileWriter(new File("output.txt")));
  7.             try {
  8.                 // for performance compile once and use repeatedly (instead of line.matches("four.*"))
  9.                 Pattern pattern = Pattern.compile("(four.*)"); // brackets only needed if using regex replace below ...
  10.                 String line;
  11.                 while ((line = reader.readLine()) != null) {
  12.                     Matcher matcher = pattern.matcher(line);
  13.                     if (matcher.matches()) {
  14.                         line = line + "6,"; // your data
  15.                         // line = matcher.replaceAll("$16,"); // or with regex-replace ... "$1" is the first "()" in the pattern above
  16.                     }
  17.                     writer.append(line);
  18.                     writer.newLine();
  19.                     // writer.flush(); // to see everything that is written immediately in the file [it's buffered otherwise]
  20.                 }
  21.             } finally {
  22.                 reader.close();
  23.             }
  24.         } finally {
  25.             reader.close();
  26.         }
  27.     }
  28.  
  29. }
Greetings Finomosec;
Jul 6 '07 #2
JosAH
11,448 Recognized Expert MVP
I'd say use a (relational) database using one table with two columns: the primary
key ("one", "two" "three" etc.) and a second column that simply contains the
text ("1, 2, 3, 4, 5" etc.).

It'll be easy to update/delete/insert data then and when you have to read data in
you could do the processing on that second column. Using plain text files is an
unmanageable burden (as you already sketched).

kind regards,

Jos
Jul 6 '07 #3
blazedaces
284 Contributor
I'd say use a (relational) database using one table with two columns: the primary
key ("one", "two" "three" etc.) and a second column that simply contains the
text ("1, 2, 3, 4, 5" etc.).

It'll be easy to update/delete/insert data then and when you have to read data in
you could do the processing on that second column. Using plain text files is an
unmanageable burden (as you already sketched).

kind regards,

Jos
Looks like I'll be looking into databases then (relational). Thank you...

-blaze
Jul 6 '07 #4
blazedaces
284 Contributor
First, there are many different XML-parsers.
- Those which load the complete file into memory (bad for big files)
- Those which walk the XML-file from top to bottom (Visitor-Pattern i think)
maybe SAX-parser, if i'm not mistaken

Greetings Finomosec;
Sorry that I was unclear dude. I have the xml reading data part down, I'm using SAX. Thing is I'm reading 200mb+ files, sometimes they can be gigabytes in size, too big to use something like DOM. Thanks though...

As for your suggestion for the code I think it might be easier to take Jos' suggestion and lookup databases, then simply print it to a file or input into something like excel (have no idea if this is possible).

Just wanted to respond to your post as well... Thanks for your input,

-blazed
Jul 6 '07 #5
r035198x
13,262 MVP
Speaking of databases MySQL 5.0 + has improved the MySQL database a lot. If you want something quick and free you can consider using that one.
Jul 7 '07 #6
JosAH
11,448 Recognized Expert MVP
Speaking of databases MySQL 5.0 + has improved the MySQL database a lot. If you want something quick and free you can consider using that one.
Personally I use Caché; it's an object oriented database that got rid of that
nasty OR mapping (Object - Relational). Relational database tables are not
well suited for storing objects. Persisting objects using this database is a
breeze; it's just like serializing Java POJOs but you can search, manipulate
them just like they are in memory all the time. No need for all those clumsy
DAO, DTO etc. patterns anymore. (Caché is free, google for it).

kind regards,

Jos

ps. I'm just a happy user, no commercial interest in Caché at all.
Jul 7 '07 #7
r035198x
13,262 MVP
Personally I use Caché; it's an object oriented database that got rid of that
nasty OR mapping (Object - Relational). Relational database tables are not
well suited for storing objects. Persisting objects using this database is a
breeze; it's just like serializing Java POJOs but you can search, manipulate
them just like they are in memory all the time. No need for all those clumsy
DAO, DTO etc. patterns anymore. (Caché is free, google for it).

kind regards,

Jos

ps. I'm just a happy user, no commercial interest in Caché at all.
My knowlegde of object oriented databases is rather limited(still in the learning phase for them I'd say).
Caché looks good. (First link I opened was a clothes shop).
Have you worked with JavaDB before?
Jul 7 '07 #8
JosAH
11,448 Recognized Expert MVP
My knowlegde of object oriented databases is rather limited(still in the learning phase for them I'd say).
Caché looks good. (First link I opened was a clothes shop).
Have you worked with JavaDB before?
I think I've played with it a bit once, but I'm not sure about it. Caché is made by
Intersystems. Those folks made "MUMPS" years ago. For reasons that are
beyond me MUMPS is still extremely popular in the medical equipment industry.
You hardly find any, say, Oracle or DB2 or whatever overthere.

kind regards,

Jos
Jul 7 '07 #9
r035198x
13,262 MVP
I think I've played with it a bit once, but I'm not sure about it. Caché is made by
Intersystems. Those folks made "MUMPS" years ago. For reasons that are
beyond me MUMPS is still extremely popular in the medical equipment industry.
You hardly find any, say, Oracle or DB2 or whatever overthere.

kind regards,

Jos
They are calling it the world's fastest database. I've requested a free cd ( I do not want to download 242mb of it on our network today)
I hope they'll send the CD.
Jul 7 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2246
by: dornick | last post by:
So I want to do the above, and I really, REALLY don't want to rewrite the entire file. I've been working on it for a while now, and can't for the life of me get it functioning. Basically, I want to replace the last text character of a certain line. So far all I've done has centered around trying to put the "put" pointer write before the character to write (ban pun, I know). But when I tried to use put(), nothing happened and a call to...
2
6847
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to replace just one item. I know I can get the entire array, then save the whole thing (with a for loop and if statements so that the changed data will be saved), but it seems like a lot of unnecessary reading and writing. Is there a way to directly save...
12
5862
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: <gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish> etc. I want to remove the "g:\" from the file paths. I wrote a console app that successfully reads the file and writes a duplicate of it, but fails for some reason to do the "replacing" of the "g:\". The code...
14
1790
by: Josh Baltzell | last post by:
I am having a lot more trouble with this than I thought I would. Here is what I want to do in pseudocode. Open c:\some.pdf Replace "Replace this" with "Replaced!" Save c:\some_edited.pdf I can do this in notepad and it works fine, but when I start getting in to reading the files I think it has some encoding problem. I tried saving the file with every encoding option. When I open a PDF in the
32
3864
by: FireHead | last post by:
Hello C World & Fanatics I am trying replace fgets and provide a equavivalant function of BufferedInputReader::readLine. I am calling this readLine function as get_Stream. In the line 4 where default_buffer_length is changed from 4 --24 code works fine. But on the same line if I change the value of default_buffer_length from 4 --10 and I get a memory error. And if the change the value of the same variable from 4 --1024 bytes;
6
5780
by: saif.shakeel | last post by:
Hi, I need to replace a string in xml file with something else.Ex - <SERVICEPARAMETER id="_775" Semantics="subfunction" DDORef="_54"> <SHORTNAME>rate</SHORTNAME> <LONGNAME>rate</LONGNAME> <VALUE role="constant" DataType="unsigned" value="1" /> <BYTEPOSITION role="position" BytePos="1" /> </SERVICEPARAMETER> - <SERVICEPARAMETER id="_776" Semantics="localId" DDORef="_54">
7
3186
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to change the font color and weight at a specific position in the text. I am not sure where or what I need to do. It will be at position 155/156 from the left on each line. Here is the code I need to put it into. using System; using System.Drawing; using System.IO;
7
4904
by: DarthBob88 | last post by:
I have to go through a file and replace any occurrences of a given string with the desired string, like replacing "bug" with "feature". This is made more complicated by the fact that I have to do this with a lot of replacements and by the fact that some of the target strings are two words or more long, so I can't just break up the file at whitespace, commas, and periods. How's the best way to do this? I've thought about using strstr() to...
2
1972
by: RoseW | last post by:
I suggested to a friend that a css controlled table would be preferrable to the existing chart produced in Excel stored at http://www.delmarcottages.ca/cottages/bookings/cottagebookings.htm They mentioned that the Excel chart is not appearing with columns correct when viewed with Firefox. My suggestion is stored at http://www.delmarcottages.ca/cottages/bookings/booking2.html and the booking2.css is in that directory also. (this page...
0
7955
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7883
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8019
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8251
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5418
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3873
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2391
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.