你这个用个Java文件测试下就行了,不用spring
解决方案 »
- JAVA 遍历表单Iterator!
- SQL 语句难题!
- struts中的forward位置问题!!!!!!郁闷!!!!
- 我想查看tomcat源码,怎么把tomcat5.5.15的源码导入eclipse工作空间或导成eclipse项目?
- webservice布置于weblogic(linux)下,客户端调用出错
- 请教各位大神,memcached的问题
- JNDI绑定问题,在线等(刚才的这个问题可能没说清楚)。。。。。。。。。。。。。。
- 某公司笔式题,欢迎参加有分讨论!
- 请问在Apache+Tomcat环境下能支持EJB吗?各位高手能告诉我J2EE的环境怎么配置?
- maven pom配置junit4.10 cmd命令报错、求解决啊
- struts2的页面传值问题
- J2EE怎么能学好它
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;public final class AccessTextFile { /**
* 1. 演示将流中的文本读入一个 StringBuffer 中
* @throws IOException
*/
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 = 1; // 三种测试的选择开关
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");
test.writeFromBuffer(buffer, System.out);
break;
}
case 3: // 测试拷贝
{
test.copyTextFile("E:\\test.txt", "E:\\r.txt");
}
break;
}
}}
public class ReadFile { public ReadFile() {
} public static void main(String[] args) throws Exception {
// TODO code application logic here
String path=System.getProperty("user.dir");
FileReader fr=new FileReader(path + System.getProperty("file.separator") +"read.txt");
//关键在于读取过程中,要判断所读取的字符是否已经到了文件的末尾,并且这个字符是不是文件中的断行符,即判断该字符值是否为13。
int c=fr.read();//从文件中读取一个字符
//判断是否已读到文件结尾
while(c!=-1){
System.out.print((char)c);//输出读到的数据
c=fr.read();//从文件中继续读取数据
if(c==13){//判断是否为断行字符
System.out.println();//输出分行标签
fr.skip(1);//略过一个字符
//c=fr.read();//读取一个字符
}
}
fr.close();
}
}
{
public static void main(String[] args)
{
BufferedReader in=new BufferedReader(new FileReader("E:\\test.txt"));
String s;
int i=0;
while((s=in.readLine())!=null)
{
StringTokenizer t=new StringTokenizer(s,"|");//"|"为分隔符
p[i].id=Integer.parseInt(t.nextToken());//将第一个记录赋给变量id
P[i].name=t.nextToken();//将第二个记录赋给变量name
P[i].age=Integer.parseInt(t.nextToken());//将第三个记录赋给变量age
P[i].sex=t.nextToken();//将第四个记录赋给变量sex
i++;
}
in.close();
}
}
class P {
public int id;
public String name;
public int age;
public String sex; }是这样写吗