反向文件java程序
我正试图让一个程序运行起来。输入是一个包含多行文本的源文件。输出是一个目标文件,具有原始文本行,但格式相反反向文件java程序,java,Java,我正试图让一个程序运行起来。输入是一个包含多行文本的源文件。输出是一个目标文件,具有原始文本行,但格式相反 ex. abcd --> dcba efgh hgfe 1234 4321 我已经研究了几个类似的问题,但是他们以一种不同于我的方式来解决这个问题,这并不能完全解决这个问题。我已经看完了,我想我只是想得太多了。如果您能告诉我为什么我的代码没有输出到目标文件,我将不胜感激。我做了一个堆栈跟踪,它一路打印得非常好 谢谢 代码: (命令行参数:source
ex.
abcd --> dcba
efgh hgfe
1234 4321
我已经研究了几个类似的问题,但是他们以一种不同于我的方式来解决这个问题,这并不能完全解决这个问题。我已经看完了,我想我只是想得太多了。如果您能告诉我为什么我的代码没有输出到目标文件,我将不胜感激。我做了一个堆栈跟踪,它一路打印得非常好
谢谢
代码:
(命令行参数:source2.txt target2.txt)
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java. util.Scanner;
/**
This program copies one file to another.
*/
public class Reverse
{
public static void main(String[] args) throws IOException
{
try{
String source = args[0];
String target = args[1];
File sourceFile=new File(source);
Scanner content=new Scanner(sourceFile);
PrintWriter pwriter =new PrintWriter(target);
while(content.hasNextLine())
{
String s=content.nextLine();
StringBuffer buffer = new StringBuffer(s);
buffer=buffer.reverse();
String rs=buffer.toString();
pwriter.println(rs);
}
content.close();
pwriter.close();
}
catch(Exception e){
System.out.println("Something went wrong");
}
}
}
您看到了什么输出 PrintWriter取消显示
IOException
,并设置错误标志;您应该使用
OutputStreamWriter()
此类中的方法从不抛出I/O异常,尽管它的一些构造函数可能会抛出I/O异常。客户端可以通过调用checkError()查询是否发生了任何错误
另外,不要处理带有“出错”的异常;至少要转储堆栈跟踪,以便知道出错的原因和位置
也就是说,我可能会将读取的每一行输出到控制台,如下所示:
System.out.println("** Read ["+s+"]");
确认我确实在读文件。您看到了什么输出
import java.io.*;
import java.util.*;
class Driver {
public static void main(String[] args) {
ReverseFile demo = new ReverseFile();
demo.readFile("source2.txt");
demo.reverse("target2.txt");
}
}
class ReverseFile {
// Declare a stream of input
DataInputStream inStream;
// Store the bytes of input file in a String
ArrayList<Character> fileArray = new ArrayList<Character>();
// Store file sizes to see how much compression we get
long inFileSize;
long outFileSize;
// Track how many bytes we've read. Useful for large files.
int byteCount;
public void readFile(String fileName) {
try {
// Create a new File object, get size
File inputFile = new File(fileName);
inFileSize = inputFile.length();
// The constructor of DataInputStream requires an InputStream
inStream = new DataInputStream(new FileInputStream(inputFile));
}
// Oops. Errors.
catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(0);
}
// Read the input file
try {
// While there are more bytes available to read...
while (inStream.available() > 0) {
// Read in a single byte and store it in a character
char c = (char)inStream.readByte();
if ((++byteCount)% 1024 == 0)
System.out.println("Read " + byteCount/1024 + " of " + inFileSize/1024 + " KB...");
// Print the characters to see them for debugging purposes
//System.out.print(c);
// Add the character to an ArrayList
fileArray.add(c);
}
// clean up
inStream.close();
System.out.println("Done!!!\n");
}
// Oops. Errors.
catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
// Print the ArrayList contents for debugging purposes
//System.out.println(fileArray);
}
public void reverse(String fileName) throws IOException {
FileWriter output = new FileWriter(fileName);
for (int i = fileArray.size() - 1; i >= 0; i++) {
try {
output.write(fileArray.get(i));
}
catch (IOException e) {
e.printStackTrace();
}
}
output.close();
}
}
PrintWriter取消显示IOException
,并设置错误标志;您应该使用
OutputStreamWriter()
此类中的方法从不抛出I/O异常,尽管它的一些构造函数可能会抛出I/O异常。客户端可以通过调用checkError()查询是否发生了任何错误
另外,不要处理带有“出错”的异常;至少要转储堆栈跟踪,以便知道出错的原因和位置
也就是说,我可能会将读取的每一行输出到控制台,如下所示:
System.out.println("** Read ["+s+"]");
确认我确实在读取文件。import java.io.*;
import java.io.*;
import java.util.*;
class Driver {
public static void main(String[] args) {
ReverseFile demo = new ReverseFile();
demo.readFile("source2.txt");
demo.reverse("target2.txt");
}
}
class ReverseFile {
// Declare a stream of input
DataInputStream inStream;
// Store the bytes of input file in a String
ArrayList<Character> fileArray = new ArrayList<Character>();
// Store file sizes to see how much compression we get
long inFileSize;
long outFileSize;
// Track how many bytes we've read. Useful for large files.
int byteCount;
public void readFile(String fileName) {
try {
// Create a new File object, get size
File inputFile = new File(fileName);
inFileSize = inputFile.length();
// The constructor of DataInputStream requires an InputStream
inStream = new DataInputStream(new FileInputStream(inputFile));
}
// Oops. Errors.
catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(0);
}
// Read the input file
try {
// While there are more bytes available to read...
while (inStream.available() > 0) {
// Read in a single byte and store it in a character
char c = (char)inStream.readByte();
if ((++byteCount)% 1024 == 0)
System.out.println("Read " + byteCount/1024 + " of " + inFileSize/1024 + " KB...");
// Print the characters to see them for debugging purposes
//System.out.print(c);
// Add the character to an ArrayList
fileArray.add(c);
}
// clean up
inStream.close();
System.out.println("Done!!!\n");
}
// Oops. Errors.
catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
// Print the ArrayList contents for debugging purposes
//System.out.println(fileArray);
}
public void reverse(String fileName) throws IOException {
FileWriter output = new FileWriter(fileName);
for (int i = fileArray.size() - 1; i >= 0; i++) {
try {
output.write(fileArray.get(i));
}
catch (IOException e) {
e.printStackTrace();
}
}
output.close();
}
}
导入java.util.*;
类驱动程序{
公共静态void main(字符串[]args){
ReverseFile演示=新建ReverseFile();
readFile(“source2.txt”);
demo.reverse(“target2.txt”);
}
}
类反转文件{
//声明输入流
流内数据输入流;
//将输入文件的字节存储在字符串中
ArrayList fileArray=新的ArrayList();
//存储文件大小以查看压缩量
长浸润;
长出料口尺寸;
//跟踪我们读取的字节数。对于大型文件很有用。
int字节数;
公共void readFile(字符串文件名){
试一试{
//创建新文件对象,获取大小
文件输入文件=新文件(文件名);
infiresize=inputFile.length();
//DataInputStream的构造函数需要InputStream
inStream=newdatainputstream(newfileinputstream(inputFile));
}
//哦,错误。
catch(filenotfounde异常){
e、 printStackTrace();
系统出口(0);
}
//读取输入文件
试一试{
//虽然有更多的字节可读取。。。
while(inStream.available()>0){
//读入单个字节并将其存储在字符中
char c=(char)inStream.readByte();
如果((+字节计数)%1024==0)
System.out.println(“读取“+infiresize/1024+”KB…”中的“+byteCount/1024+”);
//打印字符以查看它们以进行调试
//系统输出打印(c);
//将字符添加到ArrayList
添加(c);
}
//清理
流内关闭();
System.out.println(“完成!!!\n”);
}
//哦,错误。
捕获(IOE异常){
e、 printStackTrace();
系统出口(0);
}
//打印ArrayList内容以进行调试
//System.out.println(fileArray);
}
公共void reverse(字符串文件名)引发IOException{
FileWriter输出=新的FileWriter(文件名);
对于(int i=fileArray.size()-1;i>=0;i++){
试一试{
write(fileArray.get(i));
}
捕获(IOE异常){
e、 printStackTrace();
}
}
output.close();
}
}
如果不行,请告诉我,我会进一步研究这个问题。import java.io.*;
导入java.util.*;
类驱动程序{
公共静态void main(字符串[]args){
ReverseFile演示=新建ReverseFile();
readFile(“source2.txt”);
demo.reverse(“target2.txt”);
}
}
类反转文件{
//声明输入流
流内数据输入流;
//将输入文件的字节存储在字符串中
ArrayList fileArray=新的ArrayList();
//存储文件大小以查看压缩量
长浸润;
长出料口尺寸;
//跟踪我们读取的字节数。对于大型文件很有用。
int字节数;
公共void readFile(字符串文件名){
试一试{
//创建新文件对象,获取大小
文件输入文件=新文件(文件名);
infiresize=inputFile.length();
//DataInputStream的构造函数需要InputStream
inStream=newdatainputstream(newfileinputstream(inputFile));
}
//哦,错误。
catch(filenotfounde异常){
e、 printStackTrace();
系统出口(0);
}
//读取输入文件
试一试{
//虽然有更多的字节可读取。。。
while(inStream.available()>0){
//读入单个字节并将其存储在字符中
char c=(char)inStream.readByte();
如果((+字节计数)%1024==0)
System.out.println(“读取“+infiresize/1024+”KB…”中的“+byteCount/1024+”);
//打印字符以查看它们以进行调试
//系统输出打印(c);
//将字符添加到ArrayList
添加(c);
}
//清理
流内关闭();
System.out.println(“完成!!!\n”);
}
//哦,错误。