java解析java文件
1. java文件读取
String
path
=
System.getProperty("user.dir")//得到当前工作源路径
File
fileData;
FileReader
fr;
BufferedReader
br;
fileData=new
File(path,file);
//file如1.txt文件名.需要在path即工作路径下.path为工作路径
也可以改为其他
try
{
fr=new
FileReader(fileData);
br=new
BufferedReader(fr);
}
catch
(Exception
ex)
{
ex.printStackTrace();
}
String
result
=
br.readLine();
然后将result中数据(如果为数字则可将字符取出转化为数字再排序)操作
2. 有什么好的解析java文件的方法
java读取word文档时,虽然网上介绍了很多插件poi、java2Word、jacob、itext等等,poi无法读取格式(新的版API估 计行好像还在处于研发阶段权,不太稳定,做项目不太敢用);java2Word、jacob容易报错找不到注册,比较诡异,我曾经在不同的机器上试过,...
3. 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();
}
}
4. Java解析文件急急急!!!!
FileInputStream f = null;
try {
f = new FileInputStream("文件路径");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader dr = new BufferedReader(new InputStreamReader(f));
String str = null;
try {
while ((str = dr.readLine()) != null) {
String[] items = str.split(" ");
// 处理拆分出来的项
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
5. Java读取文件
你能截图你的文本文件过来么,看你的粘贴的格式,不知道哪几个是一行的。。。
6. java文件如何读取
有几种方法读取吧
File
file
=
new
File("d:\\a.txt");//把D盘目录下的a.txt读取出来,
InputStream
is
=
new
FileInputStream(file);//把文件以字节流读到内存中
第二种是类加载
Demo1.class.getClassLoader().getResourceAsStream("a.txt");//Demo1为当前类名,a.txt在与Demo1.class在同一目录下。
还有其它的就不说了
7. 怎么用java来解析file.java
用文件流读取file然后分析
8. 如何用JAVA解析txt文件
java读取txt文件内容。可以作如下理解:
首先获得一个文件句柄。File file = new File(); file即为文件句柄。两人之间连通电话网络了。接下来可以开始打电话了。
通过这条线路读取甲方的信息:new FileInputStream(file) 目前这个信息已经读进来内存当中了。接下来需要解读成乙方可以理解的东西
既然你使用了FileInputStream()。那么对应的需要使用InputStreamReader()这个方法进行解读刚才装进来内存当中的数据
解读完成后要输出呀。那当然要转换成IO可以识别的数据呀。那就需要调用字节码读取的方法BufferedReader()。同时使用bufferedReader()的readline()方法读取txt文件中的每一行数据哈。
package com.campu;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class H20121012 {
/**
* 功能:Java读取txt文件的内容
* 步骤:1:先获得文件句柄
* 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
* 3:读取到输入流后,需要读取生成字节流
* 4:一行一行的输出。readline()。
* 备注:需要考虑的是异常情况
* @param filePath
*/
public static void readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
public static void main(String argv[]){
String filePath = "L:\\Apache\\htdocs\\res\\20121012.txt";
// "res/";
readTxtFile(filePath);
}
}
9. java怎么读取java工程的文件
平时写程序的时候,很多时候提示文件找不到,而抛出了异常,现在整理如下
一 相对路径的获得
说明:相对路径(即不写明时候到底相对谁)均可通过以下方式获得(不论是一般的java项目还是web项目)
String relativelyPath=System.getProperty("user.dir");
上述相对路径中,java项目中的文件是相对于项目的根目录
web项目中的文件路径视不同的web服务器不同而不同(tomcat是相对于 tomcat安装目录\bin)
二 类加载目录的获得(即当运行时某一类时获得其装载目录)
1.1)通用的方法一(不论是一般的java项目还是web项目,先定位到能看到包路径的第一级目录)
InputStream is=TestAction.class.getClassLoader().getResourceAsStream("test.txt");
(test.txt文件的路径为 项目名\src\test.txt;类TestAction所在包的第一级目录位于src目录下)
上式中将TestAction,test.txt替换成对应成相应的类名和文件名字即可
1.2)通用方法二 (此方法和1.1中的方法类似,不同的是此方法必须以'/'开头,
InputStream is=Test1.class.getResourceAsStream("/test.txt");
(test.txt文件的路径为 项目名\src\test.txt,类Test1所在包的第一级目录位于src目录下)
三 web项目根目录的获得(发布之后)
1 从servlet出发
可建立一个servlet在其的init方法中写入如下语句
ServletContext s1=this.getServletContext();
String temp=s1.getRealPath("/"); (关键)
结果形如:D:\工具\Tomcat-6.0\webapps\002_ext\ (002_ext为项目名字)
如果是调用了s1.getRealPath("")则输出D:\工具\Tomcat-6.0\webapps\002_ext(少了一个"\")
2 从httpServletRequest出发
String cp11111=request.getSession().getServletContext().getRealPath("/");
结果形如:D:\工具\Tomcat-6.0\webapps\002_ext\
四 classpath的获取(在Eclipse中为获得src或者classes目录的路径)
方法一 Thread.currentThread().getContextClassLoader().getResource("").getPath()
eg: String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println("t---"+t);
输出:t---/E:/order/002_ext/WebRoot/WEB-INF/classes/
方法二 JdomParse.class.getClassLoader().getResource("").getPath() (JdomParse为src某一个包中的类,下同)