我正在开发一个Java程序,我必须在其中读取文件的内容,然后反向打印每一行。例如文字:
Public Class Helloprinter
Public static void
在运行我的反向程序后将打印以下内容:
retnirPolleh ssalc cilbup
diov citats cilbup
这是我到目前为止所得到的:
public static void main(String[] args) throws FileNotFoundException {
// Prompt for the input and output file names
ArrayList<String> list = new ArrayList<String>();
//String reverse = "";
Scanner console = new Scanner(System.in);
System.out.print("Input file: ");
String inputFileName = console.next();
System.out.print("Output file: ");
String outputFileName = console.next();
// Construct the Scanner and PrintWriter objects for reading and writing
File inputFile = new File(inputFileName);
Scanner in = new Scanner(inputFile);
PrintWriter out = new PrintWriter(outputFileName);
String aString = "";
while(in.hasNextLine())
{
String line = in.nextLine();
list.add(line);
}
in.close();
for(int i = 0; i <list.size(); i++)
{
aString = list.get(i);
aString = new StringBuffer(aString).reverse().toString();
out.printf("%s", " " + aString);
}
out.close();
}
} 编辑:
随着罗伯特的发布,它帮助我走上了正确的方向。问题在于,它不会保留线条。
Public Class Helloprinter
Public static void
运行我的程序后成为:
retnirPolleh ssalc cilbup diov citats cilbup
它需要保持线条布局相同。所以它应该是:
retnirPolleh ssalc cilbup
diov citats cilbup
答案 0 :(得分:0)
在你的最后一个循环中为什么不向后遍历列表呢?所以:
for(int i = 0; i <list.size(); i++)
变为:
for(int i = list.size() - 1; i >=0; i--)
答案 1 :(得分:0)
只需使用字符串构建器。你是在正确的道路上。可能只是需要一些帮助。没有“单向”可以做任何事情,但你可以尝试这样的事情:
注意:这是我的输出: retnirPolleh ssalc cilbup diov citats cilbup
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Scanner;
public class Reverse {
public static void main(String[] args) {
ArrayList<String> myReverseList = null;
System.out.println("Input file: \n");
Scanner input = new Scanner(System.in);
String fileName = input.nextLine();
System.out.println("Output file: \n");
String outputFileName = input.nextLine();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(fileName));
String text = null;
myReverseList = new ArrayList<String>();
StringBuilder sb = null;
try {
while ((text = br.readLine()) != null) {
sb = new StringBuilder();
for (int i = text.length() - 1; i >= 0; i--) {
sb.append(text.charAt(i));
}
myReverseList.add(sb.toString());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Writer writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outputFileName), "utf-8"));
for (String s : myReverseList) {
writer.write("" + s + "\n");
}
} catch (IOException ex) {
// report
} finally {
try {
writer.close();
} catch (Exception ex) {
}
}
}
}
答案 2 :(得分:0)
您似乎已经知道如何读取文件,因此请为每一行调用此方法。 请注意,这是递归,它可能不是最有效的,但它很简单,它可以做你想要的。
public String reverseString(final String s) {
if (s.length() == 0)
return s;
// move chahctrachter at current position and then put it at the end of the string.
return reverseString(s.substring(1)) + s.charAt(0);
}
答案 3 :(得分:0)
你的问题在
行 out.printf("%s", " " + aString);
这不会输出换行符。我也不确定你为什么要在那里留一个空间。
应该是:
out.println( aString );
或者
out.printf("%s%n", aString);