java從文件中讀取數據
㈠ 怎麼從另一個java文件中獲取數據
在這個另外的文件里寫:
login user = new login();
String id = user.GetUserID();
System.out.println(id);
PS:java文件首字母用大寫,方法首字母用小寫.
這是標准,最好還是養成習慣好..
㈡ Java從文件中讀取數據存入一個數組中
// 直接給一個只讀一行的吧,呵呵,數字之間以空格來分隔public static void main(String[] args) throws Exception {File file = new File("src/test.txt");double[] a = getLineFromTxt(file," ");for(int i = 0; i< a.length; i++){System.out.println("a["+i+"] = "+a[i]);}} test中的數據以空格隔開 12 32 32.02 public static double[] getLineFromTxt(File file, String split) throws Exception{BufferedReader br = new BufferedReader(new FileReader(file));String firstLine = br.readLine(); //就讀第一行哦String[] arrs = firstLine.split(" ");//將字元數組轉為double數組double[] arr = new double[arrs.length];for(int i = 0; i< arr.length; i++){arr[i] = Double.parseDouble(arrs[i]);}if(br!= null){br.close();br = null;}return arr;}
㈢ 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怎樣從文件中讀取數據,復制到另一個文件中
//一下程序僅供參考
import java.io.*;
public class TestFileAccess {
public static void main(String []args){
File fileIn=new File("1.txt");
File fileOut=new File("2.txt");
int b;
try {
FileReader fr=new FileReader(fileIn);
FileWriter fw=new FileWriter(fileOut);
while((b=fr.read())!=-1)
{
fw.write(b);
}
fw.flush();
fw.close();
fr.close();
} catch(FileNotFoundException e)
{
e.printStackTrace();
} catch(IOException e)
{
e.printStackTrace();
}
}
}
㈤ java怎麼從txt文件中讀取數據
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;i<3;i++)
{
for(int j=0;j<3;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中怎麼從文件中讀出數據到JTabel
java中有四種將文件的內容讀取成字元串
方式一:
Java code
/**
* 以位元組為單位讀取文件,常用於讀二進制文件,如圖片、聲音、影像等文件。
* 當然也是可以讀字元串的。
*/
/* 貌似是說網路環境中比較復雜,每次傳過來的字元是定長的,用這種方式?*/
public String readString1()
{
try
{
//FileInputStream 用於讀取諸如圖像數據之類的原始位元組流。要讀取字元流,請考慮使用 FileReader。
FileInputStream inStream=this.openFileInput(FILE_NAME);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int length=-1;
while( (length = inStream.read(buffer) != -1)
{
bos.write(buffer,0,length);
// .write方法 SDK 的解釋是 Writes count bytes from the byte array buffer starting at offset index to this stream.
// 當流關閉以後內容依然存在
}
bos.close();
inStream.close();
return bos.toString();
// 為什麼不一次性把buffer得大小取出來呢?為什麼還要寫入到bos中呢? return new(buffer,"UTF-8") 不更好么?
// return new String(bos.toByteArray(),"UTF-8");
}
}
㈦ java中怎樣從文件里讀取數據,然後賦值給對象數組
不是很明白你的要求
for(int i=0;i <200;i++){特別是對於 這行,不明白
這個循環
while ((line = in.readLine()) != null){
System.out.println(line);
}
已經把文件從頭讀到尾了,你的循環是用來做什麼的?
讀取的數據就在System.out.println(line);
那裡處理,不明白你的String name,Long id,double result[]怎麼對應,所以不敢寫,你對應起來再說吧(不明白id對應哪個,name對應哪個,result是不是對應分數?)
㈧ java文件操作怎樣從文件中讀出數據然後寫入到另外一個文件中
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Usebuffer
{
public static void main(String args[]) throws IOException
{
FileInputStream fis;
FileOutputStream fos;
BufferedInputStream bis;
BufferedOutputStream bos;
int i;
try
{
fis=new FileInputStream("text1.txt"); //文件輸入流
bis=new BufferedInputStream(fis); //連接帶緩沖的輸入流
fos=new FileOutputStream("text2.txt"); //文件輸出流
bos=new BufferedOutputStream(fos); //連接帶緩沖的輸出流
i=bis.read(); //讀數據
while(i!=-1)
{
bos.write(i); //寫數據
bos.flush(); //強制輸出
i=bis.read();
}
fis.close();
fos.close();
bis.close();
bos.close();
}
catch(IOException e)
{
System.out.println("do not find the file");
}
}
}
參考