‘壹’ java中怎么读取DAT文件中的内容
//调用时,只要readFile("C:\test.dat");
publicStringreadFile(Stringpath)throwsIOException...{
Filefile=newFile(path);
if(!file.exists()||file.isDirectory())
thrownewFileNotFoundException();
BufferedReaderbr=newBufferedReader(newFileReader(file));
Stringtemp=null;
StringBuffersb=newStringBuffer();
temp=br.readLine();
while(temp!=null)...{
sb.append(temp+"");
temp=br.readLine();
}
returnsb.toString();
}
‘贰’ java如何读取D盘下面的所有*.dat文件!
1、获取d:\ 目录下所有的文件,需要考虑是否需要递归。这里主要用到File等类
2、对每个文件名解析,判断是否是dat文件
3、对每个dat文件读取,主要用到InputStreamReader等类
4、生成目录,主要用到File类里的API
‘叁’ java在dat文件中读取数据并进行快速排序
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Reader {
public static String[] read(String fileName) throws IOException
{
File file=new File(fileName);
int num;
StringBuffer sb=new StringBuffer();
if(!file.exists())
{
file.createNewFile();
}
DataInputStream din=new DataInputStream(new FileInputStream(file));
byte[] data=new byte[1024];
while((num=din.read(data, 0, 1024))!=-1)
{
sb.append(new String(data,0,num));
}
return sb.toString().split(",");
}
public static void main(String args[])
{
try {
String[] array=read("D://test.dat");
for(String str:array)
{
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
‘肆’ 请问如何写一个Java程序读取DAT文件里的内容
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Test4 {
public static void main(String[] args) {
String[][] arr=new String[5][6];
String fileName="c:/a.dat";
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
char[] temp=null;
int line=0;
while ((tempString = reader.readLine()) != null) {
temp=tempString.toCharArray();
for (int i = 0; i < temp.length; i++) {
arr[line][i]=temp[i]+"";
}
line+=1;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e1) {
}
}
System.out.println("数组显示:");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 6; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
String fileName="c:/a.dat"; 你自己看着改一下吧。
‘伍’ 请问怎么用Java读取.dat文件的数据
格式知道有什么用啊,你要知道字符集啊,乱码肯定是字符集不对了,要么是16进制的,你先进行16进制的转换或字符集的转换,读文本内容跟读txt文件没区别的
‘陆’ java 读取dat文件
可以通过BufferedReader 流的形式进行流缓存,之后通过readLine方法获取到缓存的内容。
BufferedReader bre = null;
try {
String file = "D:/test/test.dat";
bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流
while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环
{
System.out.println(str);//原样输出读到的内容
};
备注: 流用完之后必须close掉,如上面的就应该是:bre.close(),否则bre流会一直存在,直到程序运行结束。
‘柒’ 写一个java方法读取dat文件中的数据
String[] data = dat.sqlit("[],");
double[] d = new double[4];
for(int i = 0,int j=0;i<data.length,j<4;i++){
try{
d[j]=Double.valueOf(data[i])
j++;
}catch(Exception e){
continue;
}
}
‘捌’ java中要读取一个.dat文件,文件存储为每行四个数据,数据之间空格隔开,如何读取这个文件数据
显然的是不是,首先我们需要读取这个文件里的内容,这里每一行都是一条数据,我们可以用bufferedreader去读取行
其次,读完之后还要对每一条数据(行)处理一下,这个用string.split,根据空格分离字符串就行了
那么之后就是根据这得到的string[]封装成Model对象了
我们大概实现如下:
//读取文件
publicstaticList<String>readLines(StringfilePath)throwsException{
Filefile=newFile(filePath);
BufferedReaderreader=newBufferedReader(newFileReader(file));
StringlineStr;
List<String>strList=newArrayList<String>();
while(null!=(lineStr=reader.readLine())){
strList.add(lineStr);
}
reader.close();
returnstrList;
}
//封装数据
(Strings)throwsException{
String[]strings=s.split("");//以空格分离
try{
Modelmodel=newModel();
model.first=strings[0];
model.second=strings[1];
model.third=strings[2];
model.fourth=newSimpleDateFormat("yyyy-MM-dd").parse(strings[3]);
returnmodel;
}catch(Exceptione){
throwe;
}
}
//测试函数,无异常即读取成功
publicstaticvoidmain(String[]args){
StringfilePath="C:/Users/YSS36/Desktop/test.dat";
try{
List<String>strs=readLines(filePath);
List<Model>models=newArrayList<Model>();
for(Stringstring:strs){
models.add(convertToModel(string));
}
System.out.println(models);
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
//Model
staticclassModel{
publicStringfirst;
publicStringsecond;
publicStringthird;
publicDatefourth;
}
‘玖’ 如何用java调用dat文件
使用IO流
假设你说的DAT文件在c盘根目录下,名字为1.dat
try
FileReader f=new FileReader("c:\\1.dat");
f.read(ch,1024,1024);
catch (IOException e)
{
system.out.printin(e.toString());
}
finally
{
f.close();
}
‘拾’ JAVA中Vector从.dat文件读取对象怎么做到的,在线等,急
.dat 文件就是纯文本文件。你打开看看里面是如何存储学生信息的。掌握存储的格式,再解析出来就好了。
可以使用IO流读取,或者反序列化,成为学生对象,再存到Vector集合中。