编写一个文件传输的JAVA程序
2008-07-23 10:24
363 查看
来源:http://zhidao.baidu.com/question/29588961.html
如果文件太大不能一次全读入!!
JAVA里关于文件读写的有几十个类,不知道你想要如何实现,
以下是读文件的一个程序,如果有问题,发信息给我吧........
import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;
public class javaTest {
public static void main(String[] args) {
String file1=System.getProperty("user.dir")+"/1.txt";//文件,自己修改
FileInputStream myFile = null;
try {
myFile = new FileInputStream(file1); //
} catch(FileNotFoundException e) {
e.printStackTrace(System.err);
System.exit(1);
}
FileChannel myChannel = myFile.getChannel();
//这里定义缓冲区大小,每次读入字节数
ByteBuffer mybuf = ByteBuffer.allocate(1024);
try {
while(myChannel.read(mybuf) != -1) {
byte[] mybytes = mybuf.array();//读入的文件转为字节数组
mybuf.clear();
/**
* 在这里进行比较
* 可以通过字节对比
* 也可以把字节转成字符串再对比
*
*/
}
myFile.close();
}catch(IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
如果文件太大不能一次全读入!!
JAVA里关于文件读写的有几十个类,不知道你想要如何实现,
以下是读文件的一个程序,如果有问题,发信息给我吧........
import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;
public class javaTest {
public static void main(String[] args) {
String file1=System.getProperty("user.dir")+"/1.txt";//文件,自己修改
FileInputStream myFile = null;
try {
myFile = new FileInputStream(file1); //
} catch(FileNotFoundException e) {
e.printStackTrace(System.err);
System.exit(1);
}
FileChannel myChannel = myFile.getChannel();
//这里定义缓冲区大小,每次读入字节数
ByteBuffer mybuf = ByteBuffer.allocate(1024);
try {
while(myChannel.read(mybuf) != -1) {
byte[] mybytes = mybuf.array();//读入的文件转为字节数组
mybuf.clear();
/**
* 在这里进行比较
* 可以通过字节对比
* 也可以把字节转成字符串再对比
*
*/
}
myFile.close();
}catch(IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
相关文章推荐
- 编写一个显示“欢迎学习Java"的Java Applet程序
- Java实训——编写一个窗体程序显示日历。
- [编写高质量代码:改善java程序的151个建议]建议42,如何让一个工具类不可实例化
- 编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,并将原来文件的扩展名从.java改为.jad
- 用java编写一个程序,来验证用户输入的数是不是2的阶次方
- 用Java编写一个小程序:键盘输入圆的半径,求圆的面积
- 编写一个Java程序,在程序中包含一个Employee类,Employee类包含name、age、salary三个成员变量
- 编写java程序推荐一个好的定义log的方式——lombok
- [基础题]8.(*)按如下要求编写Java程序:(1)定义一个交通工具收费接口Charge
- 用Java和C#编写一个截屏小程序
- 编写一个JAVA程序,创建指定长度的 int 型数组,并生成 100 以内随机数为数组中的每个元素赋值,然后输出数组
- 用Java编写的:剪切一个文件夹的小程序
- 编写一个监控Zookeeper的Java程序,可以通过两种方式 :
- 【Java】实现一个根据日期判断星座程序的编写
- java 编写一个没有main方法的程序
- 编写一个完整的java程序——Complex
- 使用Swing组件编写一个支持中文文本编辑程序ChineseTextEdit.java
- java程序,编写一个方法,输出在一个字符串中,指定字符串输出的次数
- 【Java编程】编写一个计算N个整数平均值的程序
- 编写一个表达学生和课程关系的程序-Java描述