帮忙用java基础语言编写一个程序 要求如下

import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.LinkedList;import java.util.List;public class QuestionOne { / 打开一个文本文件,每次读取一行内容。将每行作为一个String读入,并将那个String对象置入一个Linkedlist中。按相反的顺序打印出...
帮忙用java基础语言编写一个程序 要求如下
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;

public class QuestionOne {
/**
* 打开一个文本文件,每次读取一行内容。将每行作为一个String读入,
* 并将那个String对象置入一个Linkedlist中。按相反的顺序打印出Linkedlist中所有的行。
* 同样也打开一个文本文件,以便将文本写入其中。将Linkedlist中的各行随同行号一起写入文件。 很急 请高手帮帮忙啊。!!!!!!!!!!!!!
*
* @throws IOException
*/
public static void main(String[] args) {
String inputFilePath = "c:/c.txt";
String outputFilePath = "c:/output.txt";

File inputFile = new File(inputFilePath);

if (!inputFile.exists()) {
System.out.println("输入文件不存在:" + inputFilePath);
return;
}
List<String> strs = new LinkedList<String>();

// 读取文件中的内容到List中
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(inputFile)));
String lineString = null;
while ((lineString = reader.readLine()) != null) {
strs.add(lineString);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("输入文件不存在:" + inputFilePath);
return;
} catch (IOException e) {
e.printStackTrace();
return;
}

// 逆向输出
System.out.println("逆向**start***");
for (int i = strs.size() - 1; i >= 0; i--) {
System.out.println(strs.get(i));
}
System.out.println("逆向**end***");
try {
PrintWriter writer = new PrintWriter(outputFilePath);
// 输出到文件
for (int i = 0; i < strs.size(); i++) {
writer.print(i);
writer.println(strs.get(i));
}
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}2010-12-26
不要急,帮你写,周一给你,你不留个邮箱?2010-12-26
//主要功能做出来
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;

public class JieXi {

private LinkedList list = new LinkedList();
private String sourcePath;
private String targetPath;

public void setSourcePath(String sourcePath) {
this.sourcePath = sourcePath;
}

public void setTargetPath(String targetPath) {
this.targetPath = targetPath;
}

public void setList() {
FileReader fr;
try {
File file = new File(sourcePath);
Runtime.getRuntime().exec("notepad " + sourcePath);
fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while (true) {
String linestr = br.readLine();
if (linestr != null) {
list.add(linestr);
} else
break;
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void intoTxtFile() {
try {
ToFile tf = new ToFile(targetPath);
tf.newFile();
setList();
for (int i = list.size(); i > 0; i--) {
tf.toTxtFile("[" + i + "]" + (String) list.get(i - 1) + "\r\n");
}
tf.close();
Runtime.getRuntime().exec("notepad " + targetPath);
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) throws FileNotFoundException {
JieXi jx = new JieXi();
jx.setSourcePath("D:\\4.txt");
jx.setTargetPath("D:\\2.txt");
jx.intoTxtFile();
}
}

/**
* 写入TXT文件类
*/
class ToFile {
private String filename = "D:\\default.txt";
private File file;
private PrintWriter pw;

public ToFile() throws FileNotFoundException {
newFile();
pw = new PrintWriter(file);
}

public ToFile(String filename) throws FileNotFoundException {
this.filename = filename;
newFile();
pw = new PrintWriter(file);
}

public void newFile() {
file = new File(filename);
if (!file.exists())
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

public void toTxtFile(String str) {
pw.write(str);
}

public void close() {
pw.flush();
pw.close();
}
}2010-12-26
mengvlog 阅读 36 次 更新于 2025-09-09 17:07:12 我来答关注问题0
  • import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.LinkedList;import java.util.List;public class QuestionOne { / 打开一个文本文件,每次读取一行内容。将每行作为一个String读入,并将那个String对象置入一个Linkedlist中。按相反的顺序打印出Link...

  • 在计算机科学领域,Java是一种广泛使用的编程语言。以下是一个简单的Java程序示例,用于输出一条问候语。首先,我们定义一个名为Hello的类。在类中,我们定义了一个名为main的方法,这是Java程序的入口点。main方法接收一个String数组作为参数。在main方法内部,我们调用了System.out.println方法来输出一条...

  • 在编写网页时,使用Java语言可以为网站增添更多功能。比如,通过编写一段简单的Java脚本,可以在用户点击按钮时弹出一个提示框。下面是一个例子,展示如何用Java编写一个网页,当用户点击按钮时,会弹出一个信息提示框显示“信息提示框”文本。 实现上述功能的代码如下所示,首先,我们需要创建一个HTML文件,...

  •  海南加宸 编一个程序,定义数组,用for循环语句,顺序输入10个实数,然后逆序输出这10个数。

    在编程中,我们常常需要处理数据的输入与输出操作。下面是一个简单的程序实例,它展示了如何定义一个数组,并使用for循环语句来顺序输入10个实数,然后逆序输出这10个数。这个程序使用了Java语言,首先导入了Scanner类以便从控制台读取输入。程序定义了一个名为Test的类,并在其中编写了main方法。在main方法...

  •  深空见闻 随机产生3个数,按从小到大排列,java基础语言

    在Java中,生成三个随机数并按从小到大排序,可以采用简单的比较方法。下面是一个示例代码:首先,定义一个整数数组nums,长度为3,用于存储随机生成的三个数:int[] nums = new int[3];使用Random类生成随机数:Random r = new Random();nums[0] = r.nextInt(100);nums[1] = r.nextInt(...

檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部