I am writing some information in a file and I want to update some part of this information. For instance:
If we assume the current context of the file is following sentences:
This cake is made by Mary. 12
Students play football.12908
She is a teacher.546
Then I want to change 12908 to 765 in that file and write two new lines.The context of the file after changes would be like this:
This cake is made by Mary. 12
Students play football. 765
She is a teacher.546
I love my mother.
The sky is bule. 897
I want to update information many times in a file. How can I do this?
If we are talking about reading and writing so few lines you can use java's class RandomAccessFile which fits exactly to your needs, but by experience i can tell that if you need to deal with a considerable amount of data IO, this is very inefficient . In that case as suggested by alfasin: "Start by reading the file line-by-line and writing each line (or corrected line) into another file, and take it from there".
Here's a tutorial on how to use RandomAccessFile.
Related
I am trying to write a program that takes the source code from a html file and manipulates the data. So far, I have been able to write code to strip all html code and output onto a .txt file which is great.
But I am struggling with the next part of my code. I am trying to model a class for entertainment review (the website I am taking info from is a blog about movie, play, and film reviews). I want to be able to make it organized so that the new .txt file will be sorted by the type of entertainment they watched (i.e. film, play, etc.), then display the cost of the movie and also show whether it was recommended or not. I have attached what some of the source code looks like below.
I really don't even know where to start here, or how to manipulate the data to be organized. I feel I am supposed to make a constructor that takes each line of data, but I could be wrong.
<p>Tonight I saw <em class="film">You Will Meet a Tall Dark Stranger,</em> a film by Woody Allen but without him anywhere in it. I'd say it's okay to see once, but not critical.... lots of intertwining relationship stories with endings you can never anticipate.
I have a txt file with 200.000 lines. I want to show in AutoCompleteTextView only city name and country. I have idea (Show only characters) How to do that?
Example of line: (4463523 Denver 35.531250 -81.029800 US).
In this example i want to show Denver and US.
Well, unfortunately, there will not be a good way except reading the whole file in (use, for example, a Scanner). Then you can, for example, store the parts you want to access quickly in a HashMap. Depends on your specific usecase and how much memory/cpu time you want to spend.
I'm using Java to code Data Mining application.
To do so I'm reading a "arff" file and run it over a model created in WEKA.
Currently I have a ARFF file with one line as the data to process and its working good.
What i'm trying to achieve is: that user input from jCheckbox or something some information, and i'll use to to run on the model. I've taught of two ways to do it.
1. Read a file without the last line, and only append it directly to the variable.
2. Delete line number (same place always) from the ARFF, build a line from user inputs, write it to ARFF, and then read the ARFF again.
Any suggestion on which (I think 1 is better) and in general how ?
Tried some code with StringBuilder but no success.
I am writing a program that will stores information about students of two year groups. There are 10 pieces of information for each student and in total there is about 120 students. I have decided to have a separate .txt file for each student for the program to read and transfer each file information into array on start up.
However the problem I am facing is that there is a lot of different types of data manipulation: student profiles and JTable for each grade. The student profile I think is very straight forward, but in the JTable I will only need to access the first 2 and the last four pieces of information. I also need to have a JComboBox that will have the name of each student and gets updated as well when new students are added.
My questions are as followed:
Is it possible to read file names of .txt files and write them into binary tree?
How do I change the text in JLabel in program run-time? (This information comes from the arrays of each student.)
Is there a simpler way to do this?
Oh and I am using NetBeans, if that makes any difference.
Needs urgent help. Thank you!
As follows
File folder = new File("D:/data");
File[] files = folder.listFiles();
Depending on the layout manager, to prevent on laying out the first window, and then having to short labels, maybe use a minimal size.
label.setText(...);
label.setMinimalSize(new Dimension(..., ...));
Yes, it might be simpler to use a database.
Your approach has some clarity but implies writing a lot of code for maintaining lists, and writing several files back, and maintaining data integrity. Coupled with swing GUI code that is an effort.
In every case make sure you make backups, and have test data and such.
Separate Model (the data) from the View (GUI).
The Problem:
I have numerous files that contain Apache web server log entries. Those entries are not in date time order and are scattered across the files. I am trying to use Pig to read a day's worth of files, group and order the log entries by date time, then write them to files named for the day and hour of the entries it contains.
Setup:
Once I have imported my files, I am using Regex to get the date field, then I am truncating it to hour. This produces a set that has the record in one field, and the date truncated to hour in another. From here I am grouping on the date-hour field.
First Attempt:
My first thought was to use the STORE command while iterating through my groups using a FOREACH and quickly found out that is not cool with Pig.
Second Attempt:
My second try was to use the MultiStorage() method in the piggybank which worked great until I looked at the file. The problem is that MulitStorage wants to write all fields to the file, including the field I used to group on. What I really want is just the original record written to the file.
The Question:
So...am I using Pig for something it is not intended for, or is there a better way for me to approach this problem using Pig? Now that I have this question out there, I will work on a simple code example to further explain my problem. Once I have it, I will post it here. Thanks in advance.
Out of the box, Pig doesn't have a lot of functionality. It does the basic stuff, but more times than not I find myself having to write custom UDFs or load/store funcs to get form 95% of the way there to 100% of the way there. I usually find it worth it since just writing a small store function is a lot less Java than a whole MapReduce program.
Your second attempt is really close to what I would do. You should either copy/paste the source code for MultiStorage or use inheritance as a starting point. Then, modify the putNext method to strip out the group value, but still write to that file. Unfortunately, Tuple doesn't have a remove or delete method, so you'll have to rewrite the entire tuple. Or, if all you have is the original string, just pull that out and output that wrapped in a Tuple.
Some general documentation on writing Load/Store functions in case you need a bit more help: http://pig.apache.org/docs/r0.10.0/udf.html#load-store-functions