Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

adding String Array in .conf(text also) file after particular line by java?

 
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for old question, & in google lots of discussion on this question but i not get right solution or path to follow...

I am trying to do small exercise ie.
I have 1 .conf which has 900 lines, in that i want to add few lines after 806 line number. those 'few lines' like 36000 lines.
For that i start from small sample codes which i get from web.
If i get success add 1 line after particular line then i can go further to how to add 10 lines, if 10 lines successfully add after particular then i will go for large number....
so i done
1.create particular file in particular location & write only 1 line in it.(Successfully)

2. I try to read file & try to find out if particular line comes then below that try to write content.


Now problem is i fail to do such small exercise that every time i try to write my own line below line which contain [Example as follows], complete file get blank & only my line write.!
I know i have to long way to achieve my solution, but at first step i got stuck. I am trying to get solution my self till i get clue from this website.
I need help, i try to understand how file handles...even from codetanch website also i try to follow instructions but no success...
Thanks.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mandar Khire wrote:I know i have to long way to achieve my solution, but at first step i got stuck. I am trying to get solution my self till i get clue from this website.


You basically have two choices:
1. Read the file into a List, do your updates on the List, and then write it back out.
2. Use a RandomAccessFile.

And of the two, I'd probably use the first unless the size of the update is so huge that it won't fit in memory.

And possibly even better than both those solutions is to use a database.

But 36,000 lines in a .conf file? I fear your problems are only just beginning, my friend...

Winston
 
Mandar Khire
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Winston Gutkowski,


But 36,000 lines in a .conf file? I fear your problems are only just beginning, my friend...


I am trying to add those lines by keeping it in array & array will insert it into the file 1 by 1. & those line nothing but 4-5 characters each one. I hope this will work if i get helps from codeanchers.

For basic steps,
I tried as below:-

So by this, already i have 1 file named mandar.conf
in that 5 lines like bellow

1
2
3

5


I try to write "4" below "3" & above "5"
But as above code run line add below "5".
SO i think i go some steps further...but not reach to first destination.>
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mandar Khire wrote:As i post question till got any reply...


I guess you missed mine then.

Winston
 
Mandar Khire
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I guess you missed mine then.


I modified that one after seen.
That 36000 lines comes from swing program which still in my mind...till the time i am trying to do with 2/3 or upto 10 lines. If really happens then i will go for huge.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mandar Khire wrote:I am trying to add those lines by keeping it in array & array will insert it into the file 1 by 1. & those line nothing but 4-5 characters each one. I hope this will work...


Possibly, but I'd still do it in memory (ie, a List) and then write the whole lot out in one go.

And it doesn't change the fact that:
1. Updating .conf files at all is not generally a great idea.
2. 36,000 lines doesn't sound like a file - .conf or otherwise - it sounds like a database.

Winston
 
Mandar Khire
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Winston Gutkowski,

And it doesn't change the fact that:
1. Updating .conf files at all is not generally a great idea.
2. 36,000 lines doesn't sound like a file - .conf or otherwise - it sounds like a database.


1. I know actually i am working on project which handle socket programming & asterisk, but by asterisk-java.jar when i try to add any particular entry it takes lots of time but if i manually change particular conf file by opening it in editor & save it, it works.
So i thought if i write small simple java program which open conf file & edit it(write particular lines after & before particular lines) & if i call only 1 command by asterisk-java api then it will be fast process.
When i add 1 entry by java program, in conf file i seen 4 lines add, so if i add 9000 entries then 36000 lines add in conf files.
Now problem is i am not clear simple java then asterisk-java, socket is too far for me.

RandomAccessFile way i feel more difficult than bufferwriter & bufferReader, filewriter & filereader way.

I am trying find way which based on very simple java techniques so any one can understand it.
 
Mandar Khire
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From yesterday till now i try to find out right way to achieve what i want.
I asked question in stackoverflow
& i search question in it like this.
So by both & many other questions from various forums i tried following program
But before that i want to say mandar.conf file something like this


1
2
3
[stations]

5



Now program is something like it read complete file & try to find out where "[stations]"
Bellow this line i am trying to add myline which i want to add. But not possible till now.
Following is code:-

Can any one help me how to get line number by comparing string so i can cross verify the file that ok i know string "[station]" at line number 4 & java program also write below that string.
 
Marshal
Posts: 74085
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving discussion as too difficult for “beginning”.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mandar Khire wrote:Now program is something like it read complete file & try to find out where "[stations]"


You're overthinking this by a country mile; and you still haven't taken my advice: read it into a List.

Here's my suggestion:Then, anywhere you like, you can find "[station]" with:
and add your lines after that to the List.

Unless your update is absolutely enormous (and I mean in the tens of megabytes), you'll be far better of doing this than trying to deal with the file directly.

Winston
 
Mandar Khire
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell Ritchie sir,
for moving this question at proper location.

Thanks Winston Gutkowski,
I done something like this

mandar.conf is something like below:-

1
2
3
[stations]

5



By this if i want to write array of lines below "[stations]" without deleting any other content then it happens in 5-6 min,
Now i am trying if 1 line add at a time then i can surely add 4 line at a time. So for that i think i should concentrate on multidimensional array(this i read in book only never use.).
So in following line at place of array1[i] i should write array of 'array of string' ie array of '4 strings'. or something else


I am trying to use Winston Gutkowski's ways, but till now i done as above. Is this wrong?
Any more clue or help?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mandar Khire wrote:I am trying to use Winston Gutkowski's ways, but till now i done as above. Is this wrong?


Yes.

My advice: StopCoding.

Write down what you need to do and get back to us with THAT. Your code is monolithic and hopelessly mixed up with all sorts of irrelevant stuff like debug statements and timings (at least I presume that's why you're calling System.nanoTime()).

Isolate the problem.

Winston
 
Mandar Khire
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks once again Winston Gutkowski,
First of all i remove all println statements from previous code, & even timing code also.

My query is

I have 1 file extension as conf. 810 lines in that.
At 806 line number contain "[stations]
below that i want to add n number's of entries by swing program which like combobox shows 90,900,9000 select one.if any thing select if we consider this as m then (m*4) lines add in particular conf file.
So if i select 9000 then 9000*4=36000 lines in particular file.
For doing this i want to measure how much nanosec utilize by system. Trying to find out program which takes minimum time to do all thing.



Result as bellow

line1
.
.
line805
[stations]
mandar added custom line1.
line808
line809
line810


Now for some reason i also want to check if string "[stations]" is not at 806 line number then java program should check & always below that write particular lines.(For purposely add some line before "[stations]" & try to run program then also it give right result)

So all this comes under 'file handling in java' So i try previous code. & it give me proper result in only 5-6 min(i add only 1 line at each entry but i add 36000 for check how much total time for 36000 lines adding).
Now i am trying to for loop runs 9000 times but add 36000 lines.
I know all thing i mix up but that's my old problem(trying to avoid it..)...coderanchers know that...(i pretend as programmer by help of various forums.)
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mandar Khire wrote:So all this comes under 'file handling in java'...


No it doesn't, IF YOU FOLLOW MY ADVICE.

Don't try to update the file directly. Put your lines in a List and update that.

You're thinking of this as a File; but your program doesn't care whether it's a File, a database, a socket, or a stream - it's information. About the only thing it cares about is that that information is in lines, each of which is a String, so it needs to preserve the order of those lines, while allowing you to insert more. And the best way to do that (for the third and last time) is a List.

So:
1. Read your file into a List.
2. Update the List.
3. Write it back out.

There are other ways to do what you want but, unless you want to use a database, none of them (I promise you) is easier than what I've described.

Winston
 
Mandar Khire
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Winston Gutkowski,


No it doesn't, IF YOU FOLLOW MY ADVICE.

Don't try to update the file directly. Put your lines in a List and update that.


Yes i got your point after trying adding '189000' lines from my old code which is very much wrong!
1. It takes 112 min for add in particular file.
2. Same time cpu utilization also upto 45-55% so other apps get hang.
3. If i stop program between running then file get lost(?) from location.

So now i realize that i have to follow your path without asking more question on old code...but still...my mind asking me 'Why that one get wrong? at which point in that code? why that not consume more time in less number line 36000 (5min)?' (curiosity!)

Now i am using code shown in older post as below

When i write complete code then i will post it here again & make clear whether it wrong or right...but now my older path i stop.
 
Mandar Khire
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks once again Winston Gutkowski,
i use List arraylist etc things in new program.
By that i can write 189000 lines in file1, 36000 line in file2, 9000 lines in file 3 & for all 3 things only in less than 14sec.
I give here example what i done as below:-

Here i write all things in function for 1 file, i can add many functions for multiple files.
Now in this program is there any mistake?
I worrying now about few things:-
1. I see when i save any text file in linux it ask me about character set like some kind of UTC or ISO...is this things matter for above program?
2. If program crash between writing of file then what happens that file?
3. If lots of thread running in particular java program & in that 1 thread for this program then should it affect on others or other thread affect on this?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mandar Khire wrote:Now in this program is there any mistake?


I presume not, if you've tested it, and it works.

You do have a lot of stuff hard-coded though - eg: the filenames.
Why don't you think about how you might make those parameters?

Also:
1. You don't need to flush() before you close().
2. Why do you check if (bw != null) before you close()?
3. You're closing the file twice, once in your try...catch block, and then again in your finally block. Pick one.
4. clear() and removeAll() are completely unnecessary (and the latter doubly so). Just let the List go out of scope.
5. You could get rid of all that try...catch...finally stuff by simply defining your method as:
public static void write_function1() throws IOException {
5. write_function1() is a really bad name for a method.

I worrying now about few things...


Don't. Get your program working FIRST. Then, if you still have other questions, come back.

Winston
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic