java题目:编写一个程序使用java的输入,输出流技术将一个文本文件内容按行读出,,

2024年11月17日 21:24
有4个网友回答
网友(1):

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
*
* @author Felly
* @date 2008年12月29日16:16:12
*
*/
public class InOut {
//调试的时候文件和class文件在同目录,不然自己加绝对路径
public final static String inFile="in.txt";//被写文件
public final static String outFile="out.txt";//被读文件

public void readAndWrite(String in,String out) throws
FileNotFoundException,IOException {
File inFile=new File(in);
File outFile=new File(out);
BufferedReader reader=new BufferedReader(new FileReader(outFile));
PrintWriter pw=new PrintWriter(new FileWriter(inFile));

String line="";
int index=0;
while((line=reader.readLine())!=null)//循环到没有内容为止
{ index++;
line=index+line;//加上行号
pw.write(line);//写入一行
}

//close
pw.close();
reader.close();

}

}

网友(2):

import java.io.*;

public class FileLineNumber {
public static void main(String[] args) throws Exception {
appendLineNumber(new File("FileLineNumber.java"), new File("a.txt"));
}

public static void appendLineNumber(File from, File to) throws Exception {
BufferedReader in = new BufferedReader(new FileReader(from));
StringBuilder sb = new StringBuilder();
String t;
int lineNumber = 1;
while((t = in.readLine()) != null) {
sb.append(lineNumber + " ");
sb.append(t);
sb.append(System.getProperty("line.separator"));
lineNumber++;
}
in.close();
BufferedWriter out = new BufferedWriter(new FileWriter(to));
out.write(sb.toString());
out.close();
}
}

网友(3):

import java.io.*;

public class IODemo {
private static String file = "d:\\test01.txt";
private static String file1 = "d:\\test02.txt";

public static void write(String content,String file) throws Exception{
byte[] cot = content.getBytes();

File f = new File(file1);
FileOutputStream out = new FileOutputStream(f);
out.write(cot);
out.close();
}

public static String read(String filePath) throws Exception{
File f= new File(filePath);
byte con[]= new byte[1024];
FileInputStream in = new FileInputStream(f);
in.read(con);
String cont = new String(con);
in.close();
return cont;
}

public static void main(String args[]) throws Exception{
//String content = "Hello Word!!!!!!!!!!!!!";
String content = read(file);
write(content,file1);
}
}

网友(4):

我卖过一个程序,和你的需要很象。不过比你的要复杂很多。把java源代码写到一个html文件中,加行号。还要关键字亮显的效果。那个程序我卖了2500分。RMB100。如果只需要这个东东,我可以给你。把邮箱留下。