导航:首页 > 编程语言 > java读文件字节

java读文件字节

发布时间:2022-07-31 22:26:22

‘壹’ 用java如何读取一个文件的指定字节位置的数据

可以使用RandomAccessFile类。例如要从100字节开始输出工作目录下的data.txt文件的类容。
package konw.test1;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class Test1
{
public static void main(String[] args)
{
long pos = 100;
try
{
String str = "";
RandomAccessFile randomAccessFile = new RandomAccessFile("data.txt", "rw");
randomAccessFile.seek(pos);//将文件流的位置移动到pos字节处
while( (str = randomAccessFile.readLine()) != null)
{
System.out.println(str);
}
randomAccessFile.close();

} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}

‘贰’ java中怎样按字节读取文件并复制到另一个文件夹

这里以字节流FileInputStream,FileOutputStream为例。代码例子如下:

importjava.io.File;
/**
*把一个文件夹中的文件复制到一个指定的文件夹
*@authoryoung
*
*/
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;

publicclassCopyFile{
publicstaticvoidmain(String[]args){
/*指定源exe文件的存放路径*/
Stringstr="f:/jdk-1_5_0_06-windows-i586-p.exe";
/*指定复制后的exe的目标路径*/
Stringstrs="e:/.exe";
/*创建输入和输出流*/
FileInputStreamfis=null;
FileOutputStreamfos=null;

try{
/*将io流和文件关联*/
fis=newFileInputStream(str);

fos=newFileOutputStream(strs);
byte[]buf=newbyte[1024*1024];
intlen;
while((len=fis.read(buf))!=-1){
fos.write(buf,0,len);

}
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
try{
fis.close();
fos.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
}

‘叁’ java读取txt文件,想自定义从第几个字节开始读和读几个字节,怎么做

1.想自定义从第几个字节开始读
使用java.io.RandomAccessFile类,可使用构造方法RandomAccessFile af=new RandomAccessFile("C:\\1.txt","r");如果想从第100个字节开始读,可使用其方法:public void seek(long pos),如af.seek(100);2.读几个字节所有的输入流都有方法:public int read(byte[] b,
int off,
int len)
假如你想一次读20个字节,可使用:byte b[] = new byte[100];input.read(b,0,20);然后使用String str = new String(b,0,20);得到你读取的内容

‘肆’ java 如何读写文件

这个问题问的太泛泛了,读写文件,首先了解什么是输入流和输出流,什么是字节流,什么是字符流。然后创建流对象,调用其方法read or write File

‘伍’ java怎么用read()每次读文件中的四个字节保存在数组中

java使用read()方法进行读文件中的四个字节保存在数组总的示例如下:


publicstaticvoidmain(String[]arg)throwsException{
BufferedReaderreader=newBufferedReader(newFileReader("E:/test.txt"));
int[]list=newint[20];
inti=0;
Stringline=null;
while((line=reader.readLine())!=null){
String[]vStrs=line.split("");
for(Stringstr:vStrs){
list[i++]=Integer.parseInt(str);
}
}
System.out.println(Arrays.toString(list));
}

‘陆’ java中字节级数据文件的读写编程

import java.util.Scanner;
import java.io.*;

class MyFile
{
MyFile(String d)
{
this.d=d;
}
void write(String path,String datafile)
{
File f=new File(this.d);
StringBuilder sb=new StringBuilder();
String savepath;
String[] strs;
BufferedOutputStream bos;
byte[] buf;

this.path=path.endsWith("\\") ? path.substring(0,path.length()-1) : path;
savepath=this.path+"\\"+datafile;
try
{
strs=f.list();
for(String str : strs)
{
sb.append(str);
sb.append("\r\n");
}
bos=new BufferedOutputStream(new FileOutputStream(savepath),MyFile.Size);
buf=sb.toString().getBytes();
bos.write(buf,0,buf.length);
//bos.flush();
bos.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void show(String datafile)
{
String fp=datafile.contains("\\") ? datafile : this.path+"\\"+datafile;
File f=new File(fp);
BufferedInputStream bis;
byte[] buf;

try
{
buf=new byte[(int)f.length()];
bis=new BufferedInputStream(new FileInputStream(f),MyFile.Size);
bis.read(buf,0,buf.length);
System.out.println(new String(buf));
bis.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
private static final int Size=8*1024;
private String d,path;
}
public class P
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
MyFile f;
String d,path,datafile;

System.out.print("请输入windows系统中某个目录的路径:");
d=sc.nextLine();
f=new MyFile(d);
System.out.print("请输入保存数据的文件的路径:");
path=sc.nextLine();
System.out.print("请输入保存数据的文件的文件名:");
datafile=sc.nextLine();
f.write(path,datafile);
f.show(datafile);
sc.close();
}
}

阅读全文

与java读文件字节相关的资料

热点内容
如何上网上设个人加密账户 浏览:44
linux打开ssh服务 浏览:78
微信位置可以加密吗 浏览:470
算法蛮力法 浏览:438
随机排练命令 浏览:147
python多进程并发 浏览:41
安卓软件安装如何躲避安全检测 浏览:647
奇幻潮翡翠台源码百度云盘 浏览:187
什么软件可以免费pdf转word 浏览:15
php正则表达式大全 浏览:394
androidntp时间 浏览:299
轮机长命令簿英文 浏览:148
oppo铃声设置被加密怎么处理 浏览:548
粤苗app图形验证码怎么填 浏览:899
管家婆架设云服务器 浏览:254
php的登录界面代码 浏览:997
php开发客户端 浏览:998
theisle测试服怎么搜服务器 浏览:447
广播PDF 浏览:218
单片机编程300例汇编百度 浏览:35