文件拷贝<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*" %>
<%
int bytesum=0;
int byteread=0;
file://读/到流中
InputStream inStream=new FileInputStream("c:/aaa.doc");
FileOutputStream fs=new FileOutputStream( "d:/aaa.doc");byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
out.println("<DT><B>"+byteread+"</B></DT>");
bytesum+=byteread;
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
inStream.close();
%>
<%@ page import="java.io.*" %>
<%
int bytesum=0;
int byteread=0;
file://读/到流中
InputStream inStream=new FileInputStream("c:/aaa.doc");
FileOutputStream fs=new FileOutputStream( "d:/aaa.doc");byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
out.println("<DT><B>"+byteread+"</B></DT>");
bytesum+=byteread;
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
inStream.close();
%>
{
public static void main( String[ ] args ) throws IOException
{
FileInputStream fis1 = new FileInputStream( "1.txt" );
int a = fis1.read( );
while( a != -1 )
{
System.out.println( (char) a );
a = fis1.read();
}
fis1.close( );
}
}改成上面那样。
{
public static void main( String[ ] args ) throws IOException
{
FileInputStream fis1 = new FileInputStream( "1.txt" );
int a = 0;
while((a=fis1.read( )) != -1 )
{
System.out.println( (char) a );
}
fis1.close( );
}
}
while((a = fis1.read())!=-1)
a = fis1.read();就行了。
这样的结果是
a
b
c
..而我想是
abcd
1234谢谢!
这样的结果是
a
b
c
..而我想是
abcd
1234谢谢!
请教!!!!!!!!!!!!
* BufferReadLine.java
*
* Created by ww on 2004/07/02
*
*/import java.io.*;public class BufferReadLine { public void FileRW(String fileRead) throws IOException {
String str = null; File fin = null;
File fout = null;
FileReader fr = null;
BufferedReader inbr = null;
String keyWords = null;
fin = new File(fileRead); if (fin.exists() && fin.isFile() && fin.canRead()) {
fr = new FileReader(fin);
inbr = new BufferedReader(fr);
}
else {
System.err.println(
fileRead + " not exists or it not a file or it can't read");
System.exit(-1);
} System.out.println("----------------------------------------------");
do {
str = inbr.readLine();
if (str != null) { System.out.println(str);
}
}
while (str != null);
//close file
inbr.close();
fr.close();
} public static void main(String[] args) {
if (args.length != 1) {
System.err.println("use: java Main <fileNameRead>");
System.exit(-1);
}
BufferReadLine filedemo = new BufferReadLine();
try {
filedemo.FileRW(args[0]);
}
catch (IOException e) {
System.out.println("file read or write error");
System.exit(-1);
}
}
}