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");
}
}
}
参考