用JAVA编写程序,将一个文本文件中的内容,以行为单位,调整为倒序排列。提供代码,谢谢!
2个回答
展开全部
你好,代码如下,可以参考下:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class FromEndRF {
public static void read(String filename) {
RandomAccessFile rf = null;
try {
rf = new RandomAccessFile(filename, "r");
long len = rf.length();
long start = rf.getFilePointer();
long nextend = start + len - 1;
String line;
rf.seek(nextend);
int c = -1;
while (nextend > start) {
c = rf.read();
if (c == '\n' || c == '\r') {
line = rf.readLine();
if (line != null) {
System.out.println(line);
}else {
System.out.println(line);
}
nextend--;
}
nextend--;
rf.seek(nextend);
if (nextend == 0) {// 当文件指针退至文件开始处,输出第一行
System.out.println(rf.readLine());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (rf != null)
rf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
read("d:\\demo.txt");
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class FromEndRF {
public static void read(String filename) {
RandomAccessFile rf = null;
try {
rf = new RandomAccessFile(filename, "r");
long len = rf.length();
long start = rf.getFilePointer();
long nextend = start + len - 1;
String line;
rf.seek(nextend);
int c = -1;
while (nextend > start) {
c = rf.read();
if (c == '\n' || c == '\r') {
line = rf.readLine();
if (line != null) {
System.out.println(line);
}else {
System.out.println(line);
}
nextend--;
}
nextend--;
rf.seek(nextend);
if (nextend == 0) {// 当文件指针退至文件开始处,输出第一行
System.out.println(rf.readLine());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (rf != null)
rf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
read("d:\\demo.txt");
}
}
展开全部
import java.io.*;
public class Sort{
public static void main(String[] args) throws IOException {
FileReader in = null;
FileWriter out = null;
BufferedReader read;
BufferedWriter write;
String temp[] = new String[100];
try {
in = new FileReader("A.txt");
out = new FileWriter("B.txt"); }
catch (FileNotFoundException e) { System.out.println("未找到文件");}
read = new BufferedReader(in);
write = new BufferedWriter(out);
String source;
int index = 0;
while((source = read.readLine()) != null){
temp[index] = source + "\n";
index++;
}
read.close();
for(int i=index-1; i>=0; i--){
write.write(temp[i]);
write.newLine();
write.flush();
}
write.close();
}
}
public class Sort{
public static void main(String[] args) throws IOException {
FileReader in = null;
FileWriter out = null;
BufferedReader read;
BufferedWriter write;
String temp[] = new String[100];
try {
in = new FileReader("A.txt");
out = new FileWriter("B.txt"); }
catch (FileNotFoundException e) { System.out.println("未找到文件");}
read = new BufferedReader(in);
write = new BufferedWriter(out);
String source;
int index = 0;
while((source = read.readLine()) != null){
temp[index] = source + "\n";
index++;
}
read.close();
for(int i=index-1; i>=0; i--){
write.write(temp[i]);
write.newLine();
write.flush();
}
write.close();
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询