Reputation: 13357
File contents:
-- file.txt --
First Line
Second Line
Desired new file contents:
Second Line
First Line
How can I invert the file contents as shown above per the following code?
String text = "First Line.\nSecond Line.";
System.out.println(text);
text = StringUtils.reverse(text);
// scan line by line and reverse each line individually?
System.out.println(text);
I understand the logic behind the solution to reverse the string then then scan each line and reverse each line individually but I'm not very good with java. Can I use StringBuffer, BufferReader?
Also, is it possible to use a util that does the second part for me (scan each line and reverse the contents) as opposed to iterating line by line?
There could also be unicode and or special characters so I'm afraid that too much custom trickery code will break the original contents.
NOTE: This is a very simple example. The file/input could get up to 10K-20K in size. After inverting file contents I need to fit the contents in a string, which I already do (when it first comes into my method) in order to run some regex's on it. So, I need the entire buffer available for that requirement not just doing operations line by line or printing out the file contents.
Upvotes: 1
Views: 74
Reputation: 17422
Yet another solution, using nothing but Java 7 API in three lines:
List<String> lines = Files.readAllLines(Paths.get("/path/to/source_file.txt"));
Collections.reverse(lines);
Files.write(Paths.get("/path/to/destination_file.txt"), lines);
If you need to specify an input charset, then change the first line like this:
List<String> lines = Files.readAllLines(Paths.get("/path/to/source_file.txt"),
Charset.forName("UTF-8"));
If you need to specify an output charset, then change the third line like this:
Files.write(Paths.get("/home/villarr/Desktop/test2.txt"), lines,
Charset.forName("UTF-8"));
UPDATE
If you already have the file loaded in a String
, then use split()
, change the first line with this one:
List<String> lines = Arrays.asList(myStringWithContent.split("\n"));
now, if you want your new file content in another String
, the conversion is somehow trivial:
StringBuilder builder = new StringBuilder();
for(String line: lines) builder.append(line).append('\n');
String myNewFileContent = builder.toString();
Upvotes: 1
Reputation: 75585
Assuming that your file can fit in memory, here is some sample code for ChthonicProject's comment.
Scanner in = new Scanner(new File(filename));
Stack<String> myStack = new Stack<String>();
while (in.hasNextLine()) myStack.push(in.nextLine());
PrintStream ps = new PrintStream(outfilename);
while (!myStack.empty()) ps.println(myStack.pop());
ps.close();
Here is some documentation for PrintStream, Scanner, and Stack.
Update: It appears that the OP updated his requirement and now wants the contents to be inside a String instead. That can be done by removing the PrintStream
above and replacing it as follows.
StringBuilder sb = new StringBuilder();
while (!myStack.empty()) sb.append(myStack.pop()+"\n");
String output = sb.toString();
Upvotes: 2
Reputation: 8366
Expanding my comment as an answer:
BufferedReader br = new BufferedReader(new FileReader(file));
Stack<String> stack = new Stack();
String line;
while ((line = br.readLine()) != null) {
stack.push(line);
}
// At this point, the lines have been read into the stack
// (assuming the file can fit into memory)
while (!stack.empty()){
System.out.println(stack.pop());
}
br.close();
Upvotes: 2
Reputation: 4252
Perform the following steps :
Colections.reverse(list);
Upvotes: 2