??xml version="1.0" encoding="utf-8" standalone="yes"?>欧美freesex黑人又粗又大,伊人久久亚洲热,福利网站在线观看http://www.aygfsteel.com/wjywilliam/category/17471.htmlzh-cnSun, 11 Mar 2007 13:56:46 GMTSun, 11 Mar 2007 13:56:46 GMT60眀h文集之数据格式化1.0http://www.aygfsteel.com/wjywilliam/archive/2006/11/20/82327.htmlwjywilliamwjywilliamMon, 20 Nov 2006 08:56:00 GMThttp://www.aygfsteel.com/wjywilliam/archive/2006/11/20/82327.htmlhttp://www.aygfsteel.com/wjywilliam/comments/82327.htmlhttp://www.aygfsteel.com/wjywilliam/archive/2006/11/20/82327.html#Feedback0http://www.aygfsteel.com/wjywilliam/comments/commentRss/82327.htmlhttp://www.aygfsteel.com/wjywilliam/services/trackbacks/82327.html阅读全文

wjywilliam 2006-11-20 16:56 发表评论
]]>
Java的IOpȝ基本应用http://www.aygfsteel.com/wjywilliam/archive/2006/11/19/82089.htmlwjywilliamwjywilliamSun, 19 Nov 2006 12:19:00 GMThttp://www.aygfsteel.com/wjywilliam/archive/2006/11/19/82089.htmlhttp://www.aygfsteel.com/wjywilliam/comments/82089.htmlhttp://www.aygfsteel.com/wjywilliam/archive/2006/11/19/82089.html#Feedback0http://www.aygfsteel.com/wjywilliam/comments/commentRss/82089.htmlhttp://www.aygfsteel.com/wjywilliam/services/trackbacks/82089.html Java的IOpȝ基本应用

1. stream
stream代表的是M有能力出数据的数据源,或是M有能力接收数据的接收源。在Java的IO中,所有的streamQ包括Input和Out streamQ都包括两种cdQ?br /> 
 1.1 以字节ؓ导向的stream
 以字节ؓ导向的streamQ表CZ字节为单位从stream中读取或往stream中写入信息。以字节为导向的stream包括下面几种cdQ?br /> 1) input streamQ?br /> 1) ByteArrayInputStreamQ把内存中的一个缓冲区作ؓInputStream使用
 2) StringBufferInputStreamQ把一个String对象作ؓInputStream
 3) FileInputStreamQ把一个文件作为InputStreamQ实现对文g的读取操?br /> 4) PipedInputStreamQ实Cpipe的概念,主要在线E中使用
 5) SequenceInputStreamQ把多个InputStream合ƈZ个InputStream
 2) Out stream
 1) ByteArrayOutputStreamQ把信息存入内存中的一个缓冲区?br /> 2) FileOutputStreamQ把信息存入文g?br /> 3) PipedOutputStreamQ实Cpipe的概念,主要在线E中使用
 4) SequenceOutputStreamQ把多个OutStream合ƈZ个OutStream
 
 1.2 以Unicode字符为导向的stream
 以Unicode字符为导向的streamQ表CZUnicode字符为单位从stream中读取或往stream中写入信息。以Unicode字符为导向的stream包括下面几种cdQ?br /> 1) Input Stream
 1) CharArrayReaderQ与ByteArrayInputStream对应
 2) StringReaderQ与StringBufferInputStream对应
 3) FileReaderQ与FileInputStream对应
 4) PipedReaderQ与PipedInputStream对应
 2) Out Stream
 1) CharArrayWriteQ与ByteArrayOutputStream对应
 2) StringWriteQ无与之对应的以字节为导向的stream
 3) FileWriteQ与FileOutputStream对应
 4) PipedWriteQ与PipedOutputStream对应
 以字Wؓ导向的stream基本上对有与之相对应的以字节为导向的stream。两个对应类实现的功能相同,字是在操作时的导向不同。如CharArrayReaderQ和ByteArrayInputStream的作用都是把内存中的一个缓冲区作ؓInputStream使用Q所不同的是前者每ơ从内存中读取一个字节的信息Q而后者每ơ从内存中读取一个字W?br /> 
 1.3 两种不现导向的stream之间的{?br /> InputStreamReader和OutputStreamReaderQ把一个以字节为导向的stream转换成一个以字符为导向的stream?br /> 
 2. streamd属?br /> 
 2.1 “ؓstreamd属性”的作用
 q用上面介绍的Java中操作IO的APIQ我们就可完成我们想完成的Q何操作了。但通过FilterInputStream和FilterOutStream的子c,我们可以为streamd属性。下面以一个例子来说明q种功能的作用?br /> 如果我们要往一个文件中写入数据Q我们可以这h作:
 FileOutStream fs = new FileOutStream(“test.txt?;
 然后可以通过产生的fs对象调用write()函数来往test.txt文g中写入数据了。但是,如果我们惛_现“先把要写入文g的数据先~存到内存中Q再把缓存中的数据写入文件中”的功能Ӟ上面的API没有一个能满我们的需求了。但是通过FilterInputStream和FilterOutStream的子c,为FileOutStreamd我们所需要的功能?br /> 
 2.2 FilterInputStream的各U类?br /> 
 2.2.1 用于装以字节ؓ导向的InputStream
 1) DataInputStreamQ从stream中读取基本类型(int、char{)数据?br /> 2) BufferedInputStreamQ用缓冲区
 3) LineNumberInputStreamQ会记录input stream内的行数Q然后可以调用getLineNumber()和setLineNumber(int)
 4) PushbackInputStreamQ很用刎ͼ一般用于编译器开?br /> 
 2.2.2 用于装以字Wؓ导向的InputStream
 1) 没有与DataInputStream对应的类。除非在要用readLine()时改用BufferedReaderQ否则用DataInputStream
 2) BufferedReaderQ与BufferedInputStream对应
 3) LineNumberReaderQ与LineNumberInputStream对应
 4) PushBackReaderQ与PushbackInputStream对应
 
 2.3 FilterOutStream的各U类?br /> 
 2.2.3 用于装以字节ؓ导向的OutputStream
 1) DataIOutStreamQ往stream中输出基本类型(int、char{)数据?br /> 2) BufferedOutStreamQ用缓冲区
 3) PrintStreamQ生格式化输出
 
 2.2.4 用于装以字Wؓ导向的OutputStream
 1) BufferedWriteQ与对应
 2) PrintWriteQ与对应
 
 3. RandomAccessFile

 1) 可通过RandomAccessFile对象完成Ҏ件的d操作
 2) 在生一个对象时Q可指明要打开的文件的性质QrQ只读;wQ只写;rw可读?br /> 3) 可以直接跛_文g中指定的位置
 
 4. I/O应用的一个例?br />
 import java.io.*;
 public class TestIO{
 public static void main(String[] args)
 throws IOException{
 //1.以行为单位从一个文件读取数?/font>
 BufferedReader in =
 new BufferedReader(
 new FileReader("F:\\nepalon\\TestIO.java"));
 String s, s2 = new String();
 while((s = in.readLine()) != null)
 s2 += s + "\n";
 in.close();
 
 //1b. 接收键盘的输?br /> BufferedReader stdin =
 new BufferedReader(
 new InputStreamReader(System.in));
 System.out.println("Enter a line:");
 System.out.println(stdin.readLine());
 
 //2. 从一个String对象中读取数?/font>
 StringReader in2 = new StringReader(s2);
 int c;
 while((c = in2.read()) != -1)
 System.out.println((char)c);
 in2.close();
 
 //3. 从内存取出格式化输入
 try{
 DataInputStream in3 =
 new DataInputStream(
 new ByteArrayInputStream(s2.getBytes()));
 while(true)
 System.out.println((char)in3.readByte());
 }
 catch(EOFException e){
 System.out.println("End of stream");
 }
 
 //4. 输出到文?br /> try{
 BufferedReader in4 =
 new BufferedReader(
 new StringReader(s2));
 PrintWriter out1 =
 new PrintWriter(
 new BufferedWriter(
 new FileWriter("F:\\nepalon\\ TestIO.out")));
 int lineCount = 1;
 while((s = in4.readLine()) != null)
 out1.println(lineCount++ + "Q? + s);
 out1.close();
 in4.close();
 }
 catch(EOFException ex){
 System.out.println("End of stream");
 }
 
 //5. 数据的存储和恢复
 try{
 DataOutputStream out2 =
 new DataOutputStream(
 new BufferedOutputStream(
 new FileOutputStream("F:\\nepalon\\ Data.txt")));
 out2.writeDouble(3.1415926);
 out2.writeChars("\nThas was pi:writeChars\n");
 out2.writeBytes("Thas was pi:writeByte\n");
 out2.close();
 DataInputStream in5 =
 new DataInputStream(
 new BufferedInputStream(
 new FileInputStream("F:\\nepalon\\ Data.txt")));
 BufferedReader in5br =
 new BufferedReader(
 new InputStreamReader(in5));
 System.out.println(in5.readDouble());
 System.out.println(in5br.readLine());
 System.out.println(in5br.readLine());
 }
 catch(EOFException e){
 System.out.println("End of stream");
 }
 
 //6. 通过RandomAccessFile操作文g
 RandomAccessFile rf =
 new RandomAccessFile("F:\\nepalon\\ rtest.dat", "rw");
 for(int i=0; i<10; i++)
 rf.writeDouble(i*1.414);
 rf.close();
 
 rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "r");
 for(int i=0; i<10; i++)
 System.out.println("Value " + i + "Q? + rf.readDouble());
 rf.close();
 
 rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "rw");
 rf.seek(5*8);
 rf.writeDouble(47.0001);
 rf.close();
 
 rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat", "r");
 for(int i=0; i<10; i++)
 System.out.println("Value " + i + "Q? + rf.readDouble());
 rf.close();
 }
 }
 
 关于代码的解释(以区为单位)Q?/strong>
 1ZQ当d文gӞ先把文g内容d~存中,当调用in.readLine()Ӟ再从~存中以字符的方式读取数据(以下U“缓存字节读取方式”)?br /> 1bZQ由于想以缓存字节读取方式从标准IOQ键盘)中读取数据,所以要先把标准IOQSystem.inQ{换成字符导向的streamQ再q行BufferedReader装?br /> 2ZQ要以字W的形式从一个String对象中读取数据,所以要产生一个StringReadercd的stream?br /> 4ZQ对String对象s2d数据Ӟ先把对象中的数据存入~存中,再从~冲中进行读取;对TestIO.out文gq行操作Ӟ先把格式化后的信息输出到~存中,再把~存中的信息输出到文件中?br /> 5ZQ对Data.txt文gq行输出Ӟ是先把基本类型的数据输出屋缓存中Q再把缓存中的数据输出到文g中;Ҏ件进行读取操作时Q先把文件中的数据读取到~存中,再从~存中以基本cd的Ş式进行读取。注意in5.readDouble()q一行。因为写入第一个writeDouble()Q所以ؓ了正显C。也要以基本cd的Ş式进行读取?br /> 6区是通过RandomAccessFilecd文gq行操作?/p>



wjywilliam 2006-11-19 20:19 发表评论
]]>
java文g操作大全(Jsp?http://www.aygfsteel.com/wjywilliam/archive/2006/11/19/82086.htmlwjywilliamwjywilliamSun, 19 Nov 2006 12:17:00 GMThttp://www.aygfsteel.com/wjywilliam/archive/2006/11/19/82086.htmlhttp://www.aygfsteel.com/wjywilliam/comments/82086.htmlhttp://www.aygfsteel.com/wjywilliam/archive/2006/11/19/82086.html#Feedback0http://www.aygfsteel.com/wjywilliam/comments/commentRss/82086.htmlhttp://www.aygfsteel.com/wjywilliam/services/trackbacks/82086.html Java文g操作大全

文g的徏?查与删除
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>文g的徏立、检查与删除</title>
</head>
<body>
<%
String path=request.getRealPath("");
//out.println(path);
File f=new File(path,"File.txt");
//out.println(f);
//out.println(f.exists());

if(f.exists()){//查File.txt是否存在
f.delete();//删除File.txt文g
out.println(path + "\\File.txt 存在Q已删除?);
}else{
f.createNewFile();//在当前目录下建立一个名为File.txt的文?
out.println(path + "\\File.txt 不存在,已徏立?);//输出目前所在的目录路径
}
%>

目录的徏?查与删除
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>目录的徏?查与删除</title>
</head>
<body>
<%
String path=request.getRealPath("");
path=path + "\\Sub";//要建立的目录\?
File d=new File(path);//建立代表Sub目录的File对象Qƈ得到它的一个引?
if(d.exists()){//查Sub目录是否存在
d.delete();
out.println("Sub目录存在Q已删除");
}else{
d.mkdir();//建立Sub目录
out.println("Sub目录不存在,已徏?);
}
%>
</body>
</html>


如何在JSP中处理虚拟目?/strong>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>JSP中如何处理虚拟目?lt;/title>
</head>
<body>
取得虚拟目录对应的磁盘\?lt;br>
Web站点ȝ录的位置?lt;font color=#ff0000><%=request.getRealPath("/")%></font><br>
JSP|页所在的目录位置<font color=#ff0000><%=request.getRealPath("./")%></font><br>
JSP|页所在目录上一层目录的位置<font color=#ff0000><%=request.getRealPath("../")%></font><br>
</body>
</html>


文g属性的取得

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.Date,java.io.*"%>
<html>
<head>
<title>文g属性的取得</title>
</head>
<body>
<%
String path=request.getRealPath("/");
File f=new File(path,"ReadData.txt");
if(f.exists()){
%>
<%=f.getName()%>的属性如下:<br><br>
文g长度为:<%=f.length()%>
<%=f.isFile()?"是文?:"不是文g"%><br>
<%=f.isDirectory()?"是目?:"不是目录"%><br>
<%=f.canRead()?"可读?:"不可d"%><br>
<%=f.canWrite()?"可写?:"不可写入"%><br>
<%=f.isHidden()?"是隐藏文?:"不是隐藏文g"%><br>
文g的最后修Ҏ期ؓQ?lt;%=new Date(f.lastModified())%><br>
<%
}else{
f.createNewFile();//在当前目录下建立一个名为ReaData.txt的文?
%>
<%=f.getName()%>的属性如下:<br><br>
文g长度为:<%=f.length()%>
<%=f.isFile()?"是文?:"不是文g"%><br>
<%=f.isDirectory()?"是目?:"不是目录"%><br>
<%=f.canRead()?"可读?:"不可d"%><br>
<%=f.canWrite()?"可写?:"不可写入"%><br>
<%=f.isHidden()?"是隐藏文?:"不是隐藏文g"%><br>
文g的最后修Ҏ期ؓQ?lt;%=new Date(f.lastModified())%><br>
<%
}
%>
</body>
</html>


取出目录中文件的Ҏ
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>取出目录中文件的Ҏ--列出目录中的文g</title>
</head>
<body>
<%
String path=request.getRealPath("/");
File d=new File(path);//建立当前目录中文件的File对象
File list[]=d.listFiles();//取得代表目录中所有文件的File对象数组
out.println("<font color=#ff0000>" + path + "目录下的文gQ?lt;/font><br>");
for(int i=0;i<list.length;i++){
if(list<I>.isFile()){
out.println(list<I>.getName() + "<br>");
}
}
out.println("<br><font color=#ff0000>" + path + "目录下的目录Q?lt;/font><br>");
for(int i=0;i<list.length;i++){
if(list<I>.isDirectory()){
out.println(list<I>.getName() + "<br>");
}
}
%>
</body>
</html>


判断是否为空白文?/strong>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>判断是否为空白文?lt;/title>
</head>
<body>
<%
String path=request.getRealPath("/");
out.println(path);
FileReader fr=new FileReader(path + "\\AtEnd.txt");//建立FileReader对象Qƈ实例化ؓfr
//对FileReadercȝ成的对象使用read()ҎQ可以从字符中d下一个字W?
if(fr.read()==-1)//判断是否已读到文件的l尾
{
out.print("AtEnd.txt文g中没有数?lt;br>");
}else{
out.println("AtEnd.txt文g中有数据");
}
fr.close();
%>
</body>
</html>


d所有的文g数据

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*,java.lang.*"%>
<html>
<head>
<title>d所有的文g数据</title>
</head>
<body>
<%
String path=request.getRealPath(".");
FileReader fr=new FileReader(path + "\\ReadData.txt");
//关键在于dq程中,要判断所d的字W是否已l到了文件的末尾Qƈ且这个字W是不是文g中的断行W,卛_断该字符值是否ؓ13?
int c=fr.read();//从文件中d一个字W?
//判断是否已读到文件结?
while(c!=-1){
out.print((char)c);//输出d的数?
c=fr.read();//从文件中l箋d数据
if(c==13){//判断是否为断行字W?
out.print("<br>");//输出分行标签
fr.skip(1);//略过一个字W?
//c=fr.read();//d一个字W?
}
}
fr.close();
%>
</body>
</html>


一行一行读取数?
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>文gd</title>
</head>
<body>
<%
String path=request.getRealPath("");//取得当前目录的\?
FileReader fr=new FileReader(path + "\\file\\inc\\t.txt");//建立FileReader对象Qƈ实例化ؓfr
BufferedReader br=new BufferedReader(fr);//建立BufferedReader对象Qƈ实例化ؓbr
String Line=br.readLine();//从文件读取一行字W串
//判断d到的字符串是否不为空
while(Line!=null){
out.println(Line + "<br>");//输出从文件中d的数?
Line=br.readLine();//从文件中l箋d一行数?
}
br.close();//关闭BufferedReader对象
fr.close();//关闭文g
%>
</body>
</html>


略过文g中的字符不读?
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>略过字节不读?lt;/title>
</head>
<body>
<%
String path=request.getRealPath(".");
FileReader fr=new FileReader(path + "\\ReadData.txt");
fr.skip(2);//跌2个字?
int c=fr.read();//d一个字?
while(c!=-1){
out.print((char)c);
c=fr.read();
}
fr.close();
%>
</body>
</html>


数据写入文?
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>数据写入文?lt;/title>
</head>
<body>
<%
String path=request.getRealPath(".");
FileWriter fw=new FileWriter(path + "\\WriteData.txt");//建立FileWriter对象Qƈ实例化fw
//字W串写入文g
fw.write("大家好!");
fw.write("本书是《JSP~程技巧?);
fw.write("请多多指教!");
fw.write("email:stride@sina.com");
fw.close();

FileReader fr=new FileReader(path + "\\WriteData.txt");
BufferedReader br=new BufferedReader(fr);//建立BufferedReader对象Qƈ实例化ؓbr
String Line=br.readLine();
//d一行数?
out.println(Line + "<br>");
br.close();//关闭BufferedReader对象
fr.close();
%>
</body>
</html>


写入文件的数据分行
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>写入文件的数据分行</title>
</head>
<body>
<%
String path=request.getRealPath(".");
FileWriter fw=new FileWriter(path + "\\WriteData.txt");
BufferedWriter bw=new BufferedWriter(fw);
bw.write("大家好!");
bw.write("本书是《JSP~程技巧》?);
bw.newLine();//断行
bw.write("请多多指教!");
bw.newLine();//断行
bw.write("email: stride@sina.com");
bw.flush();//数据更新至文g
fw.close();//关闭文g?
out.println("写入文g内容为:<br>");
FileReader fr=new FileReader(path + "\\WriteData.txt");
BufferedReader br=new BufferedReader(fr);
String Line=br.readLine();//d一行数?
while(Line!=null){
out.println(Line + "<br>");
Line=br.readLine();
}
fr.close();
%>
</body>
</html>

如何数据追加写入到文g
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>写入文件的数据分行</title>
</head>
<body>
<%
String path=request.getRealPath(".");
RandomAccessFile rf=new RandomAccessFile(path + "\\WriteData.txt","rw");//定义一个类RandomAccessFile的对象,q实例化
rf.seek(rf.length());//指针移动到文g末尾
rf.writeBytes("\nAppend a line to the file!");
rf.close();//关闭文g?
out.println("写入文g内容为:<br>");
FileReader fr=new FileReader(path + "\\WriteData.txt");
BufferedReader br=new BufferedReader(fr);//d文g的BufferedRead对象
String Line=br.readLine();
while(Line!=null){
out.println(Line + "<br>");
Line=br.readLine();
}
fr.close();//关闭文g
%>
</body>
</html></I></I></I></I>



wjywilliam 2006-11-19 20:17 发表评论
]]>
վ֩ģ壺 | | μԴ| ʯɽ| | | | | ȫ| ɽ| Т| | ϴ| | | ֻ| ӳ| | ҵ| ľ| | | | | | Ǹ| | | ͳ| | | żҿ| | ֳ| | | | | | ʯ| |