急!!! 这几天正在做一个项目,其中有一个要求是读取服务器上的一个文本文件,不知道该怎么做,请大家给指点一下 急!!! 这几天正在做一个项目,其中有一个要求是读取服务器上的一个文本文件.我用的是java io操作.读出来后还要进行一行一行的判断,不知道该怎么做,请大家给指点一下 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看java编程思想第三版或第四版中讲IO的那章,哪里讲的很清楚。里面还有一个读写文本文件的程序TextFile.java,很符合你的要求。 可以网上下载,baidu-google全是! 从一个文件读取内容 写到另一个文件里 不知道能帮上你不import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class test { public static void main(String[] args) { try { FileReader input = new FileReader("D:\\test.txt"); FileWriter output = new FileWriter("D:\\temp.txt"); int c=input.read(); //获取第一个字符 是个整数 while (c!=-1) { output.write(c); System.out.print(c); c=input.read(); //获取下一个字符 自动指向下一个字符 } input.close(); output.close(); } catch (IOException e) { System.out.println(e); } }} try { FileReader input = new FileReader("D:\\test.txt"); FileWriter output = new FileWriter("D:\\temp.txt"); BufferedReader br = new BufferedReader(input); BufferedWriter bw = new BufferedWriter(output); String s = br.readLine(); while (s!=null) { bw.write(s); bw.newLine(); System.out.print(s); s=br.readLine(); //获取下一个字符 自动指向下一个字符 } br.close(); bw.close(); } catch (IOException e) { System.out.println(e); } public void readToBuffer(StringBuffer buffer, InputStream is) throws IOException { String line; // 用来保存每行读取的内容 BufferedReader reader = new BufferedReader(new InputStreamReader(is)); line = reader.readLine(); // 读取第一行 while (line != null) { // 如果 line 为空说明读完了 buffer.append(line); // 将读到的内容添加到 buffer 中 buffer.append("\n"); // 添加换行符 line = reader.readLine(); // 读取下一行 } } /** * 2. 演示将 StringBuffer 中的内容读出到流中 */ public void writeFromBuffer(StringBuffer buffer, OutputStream os) { // 用 PrintStream 可以方便的把内容输出到输出流中 // 其对象的用法和 System.out 一样 // (System.out 本身就是 PrintStream 对象) PrintStream ps = new PrintStream(os); ps.print(buffer.toString()); } /** * 3*. 从输入流中拷贝内容到输入流中 * * @throws IOException */ public void copyStream(InputStream is, OutputStream os) throws IOException { // 这个读过过程可以参阅 readToBuffer 中的注释 String line; BufferedReader reader = new BufferedReader(new InputStreamReader(is)); PrintWriter writer = new PrintWriter(new OutputStreamWriter(os)); line = reader.readLine(); while (line != null) { writer.println(line); line = reader.readLine(); } writer.flush(); // 最后确定要把输出流中的东西都写出去了 // 这里不关闭 writer 是因为 os 是从外面传进来的 // 既然不是从这里打开的,也就不从这里关闭 // 如果关闭的 writer,封装在里面的 os 也就被关了 } /** * 3. 调用 copyStream(InputStream, OutputStream) 方法拷贝文本文件 */ public void copyTextFile(String inFilename, String outFilename) throws IOException { // 先根据输入/输出文件生成相应的输入/输出流 InputStream is = new FileInputStream(inFilename); OutputStream os = new FileOutputStream(outFilename); copyStream(is, os); // 用 copyStream 拷贝内容 is.close(); // is 是在这里打开的,所以需要关闭 os.close(); // os 是在这里打开的,所以需要关闭 } public static void main(String[] args) throws IOException { int sw = 2; // 三种测试的选择开关 AccessTextFile test = new AccessTextFile(); switch (sw) { case 1: // 测试读 { InputStream is = new FileInputStream("E:/test.txt"); StringBuffer buffer = new StringBuffer(); test.readToBuffer(buffer, is); //System.out.println(buffer); // 将读到 buffer 中的内容写出来 is.close(); break; } case 2: // 测试写 { StringBuffer buffer = new StringBuffer("Only a test\\n"); OutputStream os = new FileOutputStream("c:/1.txt"); test.writeFromBuffer(buffer, os); break; } case 3: // 测试拷贝 { test.copyTextFile("E:\\\\test.txt", "E:\\\\r.txt"); } break; } } google一下 例子很多 改改就成了 如何编写一个对象,通过这个对象方法传参(数组名,数组长度)来输出数组 如何获取一个页面的创建和更新时间 求超级玛丽游戏素材 形如 public void aa(int... p) 的语句中,int... p 是什么意思? java编写的记事本的文本编辑 关于byte的问题 赋值和初始化问题 画图坐标 Think in java 问题。(请教) 这个代码user==null与null==user有区别吗? 球Java2图形设计电子书(我不要代码,也不要纯代码版的,已经有了) HELP!!!
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class test {
public static void main(String[] args) {
try {
FileReader input = new FileReader("D:\\test.txt");
FileWriter output = new FileWriter("D:\\temp.txt");
int c=input.read(); //获取第一个字符 是个整数
while (c!=-1) {
output.write(c);
System.out.print(c);
c=input.read(); //获取下一个字符 自动指向下一个字符
}
input.close();
output.close();
} catch (IOException e) {
System.out.println(e);
}
}
} try {
FileReader input = new FileReader("D:\\test.txt");
FileWriter output = new FileWriter("D:\\temp.txt");
BufferedReader br = new BufferedReader(input);
BufferedWriter bw = new BufferedWriter(output);
String s = br.readLine();
while (s!=null) {
bw.write(s);
bw.newLine();
System.out.print(s);
s=br.readLine(); //获取下一个字符 自动指向下一个字符
}
br.close();
bw.close();
} catch (IOException e) {
System.out.println(e);
}
throws IOException {
String line; // 用来保存每行读取的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
line = reader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("\n"); // 添加换行符
line = reader.readLine(); // 读取下一行
}
} /**
* 2. 演示将 StringBuffer 中的内容读出到流中
*/
public void writeFromBuffer(StringBuffer buffer, OutputStream os) {
// 用 PrintStream 可以方便的把内容输出到输出流中
// 其对象的用法和 System.out 一样
// (System.out 本身就是 PrintStream 对象)
PrintStream ps = new PrintStream(os);
ps.print(buffer.toString());
} /**
* 3*. 从输入流中拷贝内容到输入流中
*
* @throws IOException
*/
public void copyStream(InputStream is, OutputStream os) throws IOException {
// 这个读过过程可以参阅 readToBuffer 中的注释
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(os));
line = reader.readLine();
while (line != null) {
writer.println(line);
line = reader.readLine();
}
writer.flush(); // 最后确定要把输出流中的东西都写出去了
// 这里不关闭 writer 是因为 os 是从外面传进来的
// 既然不是从这里打开的,也就不从这里关闭
// 如果关闭的 writer,封装在里面的 os 也就被关了
} /**
* 3. 调用 copyStream(InputStream, OutputStream) 方法拷贝文本文件
*/
public void copyTextFile(String inFilename, String outFilename)
throws IOException {
// 先根据输入/输出文件生成相应的输入/输出流
InputStream is = new FileInputStream(inFilename);
OutputStream os = new FileOutputStream(outFilename);
copyStream(is, os); // 用 copyStream 拷贝内容
is.close(); // is 是在这里打开的,所以需要关闭
os.close(); // os 是在这里打开的,所以需要关闭
} public static void main(String[] args) throws IOException {
int sw = 2; // 三种测试的选择开关
AccessTextFile test = new AccessTextFile(); switch (sw) {
case 1: // 测试读
{
InputStream is = new FileInputStream("E:/test.txt");
StringBuffer buffer = new StringBuffer();
test.readToBuffer(buffer, is);
//System.out.println(buffer); // 将读到 buffer 中的内容写出来
is.close(); break;
}
case 2: // 测试写
{
StringBuffer buffer = new StringBuffer("Only a test\\n");
OutputStream os = new FileOutputStream("c:/1.txt");
test.writeFromBuffer(buffer, os);
break;
}
case 3: // 测试拷贝
{
test.copyTextFile("E:\\\\test.txt", "E:\\\\r.txt");
}
break;
}
}