『壹』 用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();
}
}