⑴ java中怎樣從一個文件中讀取文件信息
java讀取文件路徑、所佔空間大小等文件消息,主要是使用FileInputStream類來操作,示例如下:
importjava.io.File;
importjava.io.FileInputStream;
publicclassceshi{
publicstaticvoidmain(String[]args)throwsException{
java.io.FilelocalFile=newFile("D:\1.txt");
FileInputStreamins=newFileInputStream(localFile);
intcountLen=ins.available();
byte[]m_binArray=newbyte[countLen];
ins.read(m_binArray);
ins.close();
System.out.println(localFile.getAbsoluteFile()+""
+localFile.getFreeSpace());
}
}
運行結果如下:
⑵ java讀取txt文件
importjava.io.File;
publicclassTest{
publicstaticvoidmain(String[]args){
try{
Filefile=newFile("info.txt");
newRead().readFile(file);
}catch(Exceptione){
e.printStackTrace();
}
}
}
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileReader;
importjava.util.ArrayList;
publicclassRead{
publicvoidreadFile(Filefile){
ArrayList<String>arrayList=newArrayList<>();
try{
BufferedReaderbufferedReader=newBufferedReader(newFileReader(file));
inti=1;
Stringline=null;
Stringperson="";
while((line=bufferedReader.readLine())!=null){
String[]strings=line.split("\s+");
for(Strings:strings){
if(i!=4){
person+=s+",";
}
else{
person+=s;
arrayList.add(person);
person="";
i=0;
}
i++;
}
}
System.out.println("{");
for(i=0;i<arrayList.size();i++){
Strings=arrayList.get(i);
if(i!=arrayList.size()-1)
System.out.print("["+s+"];");
else
System.out.print("["+s+"]");
}
System.out.println("}");
}catch(Exceptione){
e.printStackTrace();
}
}
}
⑶ java解析txt里的數據
最簡單的辦法就是用Java中的BufferedReader來讀取,BufferedReader可以一行一行的讀。然後用String類的split方法按","把每行的數據分割成數組,然後數組里其它已經有創建一個Student對象包含的所有東東了。在循環的最後記得把創建出來的Student對象add到List中哦。
具體代碼上面幾位都粘出來了,我就不再重復了。純手打,希望樓主採納
⑷ java讀取整個文本文件
可以通過BufferedReader 流的形式進行流緩存,之後通過readLine方法獲取到緩存的內容。
BufferedReader bre = null;
try {
String file = "D:/test/test.txt";
bre = new BufferedReader(new FileReader(file));//此時獲取到的bre就是整個文件的緩存流
while ((str = bre.readLine())!= null) // 判斷最後一行不存在,為空結束循環
{
System.out.println(str);//原樣輸出讀到的內容
};
備註: 流用完之後必須close掉,如上面的就應該是:bre.close(),否則bre流會一直存在,直到程序運行結束。
⑸ java中怎麼解析這樣的文件
鍵值的話直接獲取節點值, 你補充的那種形式要獲取屬性名和屬性值即可。
用dom4j處理,要下載dom4j.jar包build path到項目里,然後你的xml內容要改一下,裡面用雙引號:
<?xml version="1.0" encoding="UTF-8"?>
<msg>
<head>
<transcode>9999</transcode>
<time>20090305102259</time>
<version> 1.0 </version>
</head>
<body>
<msg errorCode="9001" msg="系統異常" />
</body>
</msg>
代碼
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class GetXMLMessage {
public static void main(String[] args) throws DocumentException
{
//讀取XML文件,我直接放根目錄下
File file = new File("your.xml");
Document doc = new SAXReader().read(file);
//獲取根節點:即msg節點
Element element =doc.getRootElement();
//遍歷子節點,也即<head>和<body>。
Iterator it = element.elementIterator();
while(it.hasNext())
{
Element subelement = (Element)it.next();
System.out.println("父節點"+subelement.getName());
//遍歷head下面的transcode time cerson 和body下面的msg
Iterator subit = subelement.elementIterator();
while(subit.hasNext())
{
Element subsubelement =(Element) subit.next();
System.out.print("子節點"+subsubelement.getName()+" ");
System.out.println(subsubelement.getStringValue());
//如果有屬性,則輸出屬性
List attributelist = subsubelement.attributes();
for(Object i:attributelist)
{
Attribute attribute =(Attribute)i;
System.out.print("屬性"+attribute.getName()+" ");
System.out.println(attribute.getStringValue());
}
}
}
}
}
最後輸出:
父節點head
子節點transcode 9999
子節點time 20090305102259
子節點version 1.0
父節點body
子節點msg
屬性errorCode 9001
屬性msg 系統異常
⑹ JAVA 解析txt文件。
如果txt文件里每一行都是一個屬性的話,你一行一行讀,然後取第一個indexOf("=")的位置,查找第一個indexOf("{")和最後一個lastIndexOf("}")的位置,substring取內容就好了吧。
⑺ 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;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如何解析文件夾下的json文件
用文件流讀出文件內容,然後再解析。
⑼ 有什麼好的解析java文件的方法
java讀取word文檔時,雖然網上介紹了很多插件poi、java2Word、jacob、itext等等,poi無法讀取格式(新的API估 計行好像還在處於研發階段,不太穩定,做項目不太敢用);java2Word、jacob容易報錯找不到注冊,比較詭異,我曾經在不同的機器上試過,...