So i have a scanner that reads through a text file of many lines using while(file.hasNext()), however after it reaches the end of the text file how do I make it so that I can start reading lines from the beginning again for a separate while loop?
If you want to read multiple files (example, "text1.txt, text2.txt, text3.txt..etc"), this is what you can do:
Implement your file reading within a method such as:
public void readFile(String filename){
//all the code for file reading goes here
}
String[] filesToRead = new String[]{"text1.txt", "text2.txt", "text3.txt"};
for(int x=0; x<filesToRead.length; x++) //iterate through the file names
readFile(filesToRead[x]); //repeatedly invoked to read various files
Certianly, you may also save the file names within another text file, and just read from there. This way you don't even have to recompile your program when you want to change the list of files to be read.
Example:
FilesToRead.txt
text1.txt
text2.txt
text3.txt
Related
I need to make a database for book storing. So what I want to do Is basically make a file for each book which I have done.
Now I need to search books when I need them. In 2 functions I'll need to find the book and edit its information.
I have no idea how to get the name of a file and assign it to a scanner\bufferwriter\ect.
Can anyone help me?
You have two options:
Put the names of the files into another, known filename (ie: BookList.txt) and then when your program opens you can parse through this file and get the other file names in the directory.
You can get the file directory listing and store it into a list. For example:
File f = new File("C:\\");
ArrayList<String> names = new ArrayList<String>(Arrays.asList(f.list()));
Once you know the file names, you can open these files and make any necessary edits. Don't forget to also close your file readers and writers once you're done with them!
You can iterate through the array using a for loop. For example:
for(String i : names){ // iterate through each file name in the array
Scanner fileScanner = new Scanner(i);
... // do some parsing
fileScanner.close();
}
hello every one i have a very big challenge tody , is to split a big xml file that contains for example 2000 elements (eg) :
`<bigxml>
<element><small>AAA<small/><small>BBB<small/><small>CCC<small/></element>
<element><small>DDD<small/><small>EEE<small/><small>FFF<small/></element>
<element><small>GGG<small/><small>HHH<small/><small>III<small/></element>
.......
.......
</bigxml>`
the result is multiple files with 100 element per file without losing the header
'<bigxml> </bigxml>' per file !
how to proceede please ?
Well, the simplest thing to do is to read the file line by line and have a counter to make sure that when you read 100 lines, write it into a file with opening and closing bigxml tag and continue iteration. A simple nested loop should do the trick.
Do something like
while(scanner.hasNextLine()){
// Here open a new file
for(int i=0;i<100;i++){
String s = scanner.nextLine();
// write string into the file
}
// Close the file
}
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've been programming a text-based RPG game, and I'm trying to implement a save game feature. Everything is coded and works correctly.
It works by having a file called "slist" that holds the name of the savegame and the "session ID Number". There is then a file for each savegame. The program scans this file to see if a savefile exists or not, then determines actions from there.
Note: I know this can be simplified a lot, but want to learn that on my own.
The problem I'm running into is that I want to be able to skip lines when reading from a file using FileReader. This is so users can share files with one another, and I can add comments for them at the top of the file (see below).
I've tried using Scanner.nextLine(), but it needs to be possible to insert a certain character anywhere in the file and have it skip the line following the character (see below).
private static String currentDir = new File("").getAbsolutePath();
private static File sessionList= new File(currentDir + "\\saves\\slist.dat"); //file that contains a list of all save files
private static void readSaveNames() throws FileNotFoundException {
Scanner saveNameReader = new Scanner(new FileReader(sessionList));
int idTemp;
String nameTemp;
while (saveNameReader.hasNext()) {
// if line in file contains #, skip the line
nameTemp = saveNameReader.next();
idTemp = saveNameReader.nextInt();
saveNames.add(nameTemp);
sessionIDs.add(idTemp);
}
saveNameReader.close();
}
And the file it refers to would look something like this:
# ANY LINES WITH A # BEFORE THEM WILL BE IGNORED.
# To manually add additional save files,
# enter a new blank line and enter the
# SaveName and the SessionID.
# Example: ExampleGame 1234567890
GenericGame 1234567890
TestGame 0987654321
#skipreadingme 8284929322
JohnsGame 2718423422
Is there are way to do this, or would I have to get rid of any "comments" in the file and use a for loop to skip the top 5 lines?
My Java's a bit rusty, but...
while (saveNameReader.hasNext()) {
nameTemp = saveNameReader.next();
// if line in file contains #, skip the line
if (nameTemp.startsWith("#"))
{
saveNameReader.nextLine();
continue;
}
idTemp = saveNameReader.nextInt();
saveNames.add(nameTemp);
sessionIDs.add(idTemp);
}
I mean that , I write 100 words(every each line contains one word so I write 100 line to 100 word) in a.txt. And I want to acces this txt in my application on android. And for example I only want to 6.word .For this I want to write a function.
public String generate(int a)
{
//I want to return a. word in my txt
//
//
//
//
return ...
}
And I don't know how I do this?
Put the txt in the res/raw folder.
Let's say the file is named ff.txt.
Then just open the file as an input stream using the openRawResource() method and providing the resource id as input (e.g.: package.name.here.R.raw.ff).
Once you have the InputStream returned by the openRawResource() method you can do everything you need with the file as you would normally do in a generic java application.