本文目錄一覽:
- 1、用java怎麼從指定文件中的指定位置開始讀取指定長度的內容
- 2、在java中如何把一個文件放到指定的文件夾中
- 3、用java怎麼從指定文件中的指定位置開始讀取
- 4、java中怎麼從文件中讀取數
- 5、java中如何從文件中讀取數據
- 6、java如何在指定文件位置創建文件
用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:\java\test目錄下。
方法一:
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();
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/152625.html