首页 > 编程知识 正文

在从文件位置的java中,java向文件指定位置写入数据

时间:2023-12-27 22:26:17 阅读:324060 作者:EVNC

本文目录一览:

用java怎么从指定文件中的指定位置开始读取指定长度的内容

e.printStackTrace();}return result;}}在标准的J2SE中,实现LZ的需求,支持使用RandomAccessFile类Java code RandomAccessFile r =new RandomAccessFile(new File("c:/1.txt", "r"));//只读方式打开文件r.seek(100);//指定下一次的开始位置byte[] bs =newbyte[1024]; r.read(bs); r.readChar(); r.readInt();//读取一个整数

在java中如何把一个文件放到指定的文件夹中

首先获得fileoutput对象时,写入具体的目录就可以了。

比如:你要写入到d:javatest目录下。

方法一:

java代码

string

name

=

"out.html";

string

dir

=

"d:\java\test";

file

file

=

new

file(dir,name);

fileoutputstream

out

=

new

fileoutputstream(file);

方法二:

java代码

fileoutputstream

out

=

new

fileoutputstream(dir+"\"+name);

用java怎么从指定文件中的指定位置开始读取

FileInputStream fis = FileInputStream(File file);指定文件

fis.skip(long n);指定位置

byte[] bs = new byte[int length]; 指定长度

fis.read(bs); 得到内容

java中怎么从文件中读取数

1.package txt;

2.

3.import java.io.BufferedReader;

4.import java.io.File;

5.import java.io.FileInputStream;

6.import java.io.InputStreamReader;

7.

8./**

9. * 读取TXE数据

10. */

11.public class ReadTxtUtils {

12. public static void main(String arg[]) {

13. try {

14. String encoding = "GBK"; // 字符编码(可解决中文乱码问题 )

15. File file = new File("c:/aa.txt");

16. if (file.isFile() file.exists()) {

17. InputStreamReader read = new InputStreamReader(

18. new FileInputStream(file), encoding);

19. BufferedReader bufferedReader = new BufferedReader(read);

20. String lineTXT = null;

21. while ((lineTXT = bufferedReader.readLine()) != null) {

22. System.out.println(lineTXT.toString().trim());

23. }

24. read.close();

25. }else{

26. System.out.println("找不到指定的文件!");

27. }

28. } catch (Exception e) {

29. System.out.println("读取文件内容操作出错");

30. e.printStackTrace();

31. }

32. }

33.}

java读取TXT文件中的数据,每一行就是一个数,返回一个数组,代码?

?

List list=new ArrayList();

BufferedReader br=new BufferReader(new InputStreamReader(new FileInputStream(new File("in.txt"))));

String str=null;

while((str=br.readLine())!=null)

{

list.add(new Integer(str));

}

Integer[] i=new Integer[list.size()];

list.toArray(i);

TXT文本中如据形如:

123

456

789

读入二维数组效果为:

temp[0][]={1,2,3};

temp[1][]={4,5,6};

temp[2][]={7,8,9};

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.*;

public class xx{

public static void main(String[]args){

String s;

int[][]save=new int[3][3];

try{

BufferedReader in =new BufferedReader(new FileReader("C:\txt.txt"));

int i=0;

while((s=in.readLine())!=null){

save[i][0]=Integer.parseInt(s.substring(0,1));

save[i][1]=Integer.parseInt(s.substring(1,2));

save[i][2]=Integer.parseInt(s.substring(2,3));

i++;

}

}

catch(FileNotFoundException e){

e.printStackTrace();

}

catch(IOException e){

e.printStackTrace();

}

for(int i=0;i3;i++)

{

for(int j=0;j3;j++){

System.out.print(save[i][j]);

}

System.out.println();

}

}

}

?

BufferedReader bf=new BufferedReader(new FileReader("Your file"));

String lineContent=null;

int i = 0;

int [][] temp = new int [3][];

while((lineContent=bf.readLine())!=null){

String [] str = lineContent.split("\d");// 将 lineContent 按数字拆分

for(int j = 0; j str.length(); j++){

int [i][j] = Integer.parseInt(str[j]);

}

i++;

}

scp|cs|ff|201101

这是d:\a.txt的数据,与“|”分割取数据出来,保存在变量a;b;c;d里

import java.io.*;

public class Test{

public static void main(String[] args)throws Exception{

String a, b, c, d;

StringBuffer sb = new StringBuffer();

BufferedReader br = new BufferedReader(new FileReader("d:\a.txt"));

String s = br.readLine();

while(s != null){

sb.append(s);

s = br.readLine();

}

s = sb.toString();

String[] str = s.split("|");

a = str[0];

b = str[0];

c = str[0];

d = str[0];

}

}

java中如何从文件中读取数据

分为读字节,读字符两种读法

◎◎◎FileInputStream 字节输入流读文件◎◎◎

public class Maintest {

public static void main(String[] args) throws IOException {

File f=new File("G:\just for fun\xiangwei.txt");

FileInputStream fin=new FileInputStream(f);

byte[] bs=new byte[1024];

int count=0;

while((count=fin.read(bs))0)

{

String str=new String(bs,0,count); //反复定义新变量:每一次都 重新定义新变量,接收新读取的数据

System.out.println(str); //反复输出新变量:每一次都 输出重新定义的新变量

}

fin.close();

}

}

◎◎◎FileReader 字符输入流读文件◎◎◎

public class Maintest {

public static void main(String[] args) throws IOException {

File f=new File("H:\just for fun\xiangwei.txt");

FileReader fre=new FileReader(f);

BufferedReader bre=new BufferedReader(fre);

String str="";

while((str=bre.readLine())!=null) //●判断最后一行不存在,为空

{

System.out.println(str);

}

bre.close();

fre.close();

}

}

java如何在指定文件位置创建文件

以下代码是在D:/myDir1/myDir2下创建名为myFile.txt的文件

public static void test3() {

// 根据系统的实际情况选择目录分隔符(windows下是,linux下是/)

String separator = File.separator;

String directory = "D:" + separator + "myDir1" + separator + "myDir2";

// 以下这句的效果等同于上面两句,windows下正斜杠/和反斜杠都是可以的

// linux下只认正斜杠,为了保证跨平台性,不建议使用反斜杠(在java程序中是转义字符,用来表示反斜杠)

// String directory = "myDir1/myDir2";

String fileName = "myFile.txt";

// 在内存中创建一个文件对象,注意:此时还没有在硬盘对应目录下创建实实在在的文件

File f = new File(directory, fileName);

if (f.exists()) {

// 文件已经存在,输出文件的相关信息

System.out.println(f.getAbsolutePath());

System.out.println(f.getName());

System.out.println(f.length());

} else {

// 先创建文件所在的目录

f.getParentFile().mkdirs();

try {

// 创建新文件

f.createNewFile();

} catch (IOException e) {

System.out.println("创建新文件时出现了错误。。。");

e.printStackTrace();

}

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。