I have a text file that needs editing every time a jar runs. Here is what it initially looks like.
1.png
2.png
3.png
4.png
What I've been trying to do is use FileWriter to move the top line to the bottom, so the next line will be on top, so the result would be.
2.png
3.png
4.png
1.png
Here is the code I wrote so far:
BufferedReader reader = new BufferedReader(new FileReader("list.txt"));
FileWriter writer = new FileWriter("list.txt");
String newName = scan.nextLine();
writer.write(newName);
reader.close();
writer.close();
This won't work because the only thing present in the text will be png.2
. So I was wondering... is there a way to tell FileWriter or something else to simply move the first line to the bottom?
Glad you found a solution but just in case you want another solution you can try this method code. The method allows you to move any literal file line to any literal line location within the file.
The code is well commented. Read the doc's above the method:
/**
* Moves a text file line supplied as a literal file line number to another literal
* file line location.<br><br>
*
* A temporary file is created with the modifications done. The original file is then
* deleted and the modified temporary file is renamed to the same name that the original
* file held.<pre>
*
* <b>Example Usage:</b>
*
* {@code
* try {
* moveFileLine("list.txt", 3, 1);
* }
* catch (IOException ex) {
* Logger.getLogger(getName()).log(Level.SEVERE, null, ex);
* }
* }</pre>
*
* @param filePath (String) The full path and file name of the text file to process.<br>
*
* @param lineToMove (Integer - int) A literal file line number of the text you want
* to move. By literal we mean line 1 is considered the first line of the file (not 0).<br>
*
* @param moveToLine (Optional - Integer - int - Default is 0) A literal file line
* number of where you want to move the file line text to. If 0 or nothing is is
* supplied then the line to move is placed at the end of the file otherwise the
* text is placed at the literal file line specified. By literal we mean line 1 is
* considered the first line of the file (not 0).<br><br>
*
* If a value is supplied which is greater than the actual number of lines in file
* then that value is automatically changed to the number of lines in file which
* will therefore place the line-to-move to the end of file.<br>
*
* @throws FileNotFoundException
* @throws IOException
*/
@SuppressWarnings("null")
public void moveFileLine (String filePath, int lineToMove, int... moveToLine)
throws FileNotFoundException, IOException {
int moveTo = 0; // Default move to
// Acquire the optional move-to line number if supplied.
if (moveToLine.length > 0) {
moveTo = moveToLine[0];
}
// open a stream reader and writer.
BufferedReader reader = new BufferedReader(new FileReader(filePath));
FileWriter writer = new FileWriter("tmpMoveLineFile.txt");
String line; // The current file line text bing processed.
String newLine = System.lineSeparator(); // System Line Separator.
String moveLineString = ""; // Will hold the file line text to move.
int lineCounter = 0; // Keeps track of the current file line.
// Get and hold the file line contents we want to move.
while ((line = reader.readLine()) != null) {
lineCounter++;
if (lineCounter == lineToMove) {
moveLineString = line;
if (moveTo > 0) { break; }
}
}
// Close the reader (if it's open)
if (reader != null) { reader.close(); }
// If no Move-To line number was supplied then move the
// desired line to end of file so as to be the last line.
if (moveTo <= 0 || moveTo > lineCounter) {
moveTo = lineCounter;
}
// Set up to start reading our file from the beginning again.
reader = new BufferedReader(new FileReader(filePath));
lineCounter = 0; // Reset the line counter
// Start reading in file data one line at a time
while ((line = reader.readLine()) != null) {
lineCounter++;// increment to keep track of the current line number
// If the line counter equals the supplied line to move to then...
if (lineCounter == moveTo) {
// Ternary Operator is used here. If the current line counter
// value is greater than 1 and the current line counter is
// greater than the line to move then write the current line
// the first then the line-to-move text otherwise write the
// line-to-move first then write the current line.
writer.write((lineCounter > 1 && lineCounter > lineToMove ?
line + newLine + moveLineString + newLine :
moveLineString + newLine + line + newLine));
}
// otherwise just write the encountered line.
else if (lineCounter != lineToMove) {
writer.write(line + newLine);
}
}
// Close the reader and Writer.
writer.close();
reader.close();
// delete the original file then Rename the newly created tmp
// file to the Original File name supplied.
File origFile = new File(filePath);
if (origFile.delete()) {
File srcFile = new File("tmpMoveLineFile.txt");
srcFile.renameTo(origFile);
}
}