⑴ 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容易报错找不到注册,比较诡异,我曾经在不同的机器上试过,...