导航:首页 > 编程语言 > 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读文件字节相关的资料

热点内容
你用什么app 浏览:224
安卓平板用什么优化软件 浏览:745
centos重新编译程序 浏览:333
cocoapods命令 浏览:904
androidusb卸载 浏览:141
linux解压缩软件 浏览:141
ppt及备注转pdf 浏览:562
ssh下载文件命令 浏览:819
程序员考MBA 浏览:263
国外加密锁是什么样子 浏览:227
linuxhosts重启 浏览:168
c语言编译正确但是不能执行 浏览:856
编程素质教育代理 浏览:908
渴望力量程序员 浏览:370
app页面拆解拆到什么程度 浏览:152
如何建立备份服务器 浏览:595
python与计算机编程语言 浏览:46
ac压缩机 浏览:957
linux磁盘id 浏览:660
有什么能让狗狗翻译的app 浏览:857