Created
February 8, 2017 15:17
-
-
Save gaverdugo/9a8685effadb14ac4069d92137dad5c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package reverseread; | |
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.regex.Pattern; | |
/** | |
* Created by gava9 on 07/02/2017. | |
*/ | |
public class ReverseRead { | |
public static void main(String[] args) { | |
String home = System.getProperty("user.home"); | |
BufferedReader br; | |
File in = new File(home + "\\sample.txt"); | |
File out = new File(home + "\\output.txt"); | |
ArrayList<String> lines = new ArrayList<>(); | |
// Lee el archivo y agrega sus lineas a un arraylist | |
try { | |
br = new BufferedReader(new FileReader(in)); | |
String line; | |
while((line = br.readLine()) != null) { | |
lines.add(line); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
BufferedWriter bw; | |
String tokens[] = null; | |
String splitPattern = ""; | |
Pattern p = Pattern.compile(splitPattern); | |
ArrayList <String> reverses = new ArrayList<>(); | |
for (String l: lines) { | |
String l1 = l; | |
tokens = p.split(l); | |
for (int i = tokens.length - 1; i >= 0; i--) { | |
l+=tokens[i]; | |
} | |
reverses.add(l1 + "\t" + l); | |
} | |
try { | |
if(!out.exists()) { | |
out.createNewFile(); | |
} | |
bw = new BufferedWriter(new FileWriter(out)); | |
for (String op : reverses) { | |
bw.write(op); | |
bw.newLine(); | |
bw.newLine(); | |
} | |
bw.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment