① 如何用java程序查看.dat文件
如果你的dat文件被加密过,那么需要解密才能看,否则读出来的都是乱码,如果没有加密,那么通过io流,反正java的io流可以把里面的所有字符都读出来,方法就多了去了,到网络中查找java读文件就可以找出一堆代码
② java怎样获取指定目录下的所有“.dat”文件,并且解析获取这些文件中的数据
获取文件名很简单
但获取数据要看其内容格式,一般需要预先知道其格式才能读取
③ java如何读取D盘下面的所有*.dat文件!
1、获取d:\ 目录下所有的文件,需要考虑是否需要递归。这里主要用到File等类
2、对每个文件名解析,判断是否是dat文件
3、对每个dat文件读取,主要用到InputStreamReader等类
4、生成目录,主要用到File类里的API
④ 请问java程序生成的dat数据文件用什么软件工具可以打开查看
.dat并不是一种格式,任何格式的数据都可以存储为.dat格式
你用文本编辑器打开看看是不是普通的文本数据格式,还是其他编码格式的,用相应的方法就可以了
没有一个100%能打开所有DAT文件的软件,如果记事本打不开那怎么办呢?套用那句老话“解铃还须系铃人”,因此您得知道这个“系铃人”是谁(就是对应的程序是哪个)!下面有一些办法:
有时候我们在接受邮件附件时也会偶尔收到DAT格式文件,那么我们先尝试下记事本能否打开,如果不能就要询问发邮件的人:这是一个什么文件?如果回答是图片,您就可以尝试修改扩展名后用图片查看器打开,如果说是个视频,此时你可以尝试使用暴风影音。
对于一些奇怪的现象:一些邮件程序会自动将附件的原有格式改成DAT格式。比如一张名为mtoou.jpg的图片,它会自动改成mtoou.dat。如果你知道,那么再将dat改成jpg就可以了。如果不知道您还是需要询问发件者到底是这个DAT文件是个什么格式的。
不知不觉也打了这么多,位的就是要说明:DAT文件是个未知数,里面存放着什么只有创建它的程序才知道。如果您不知道它是否是别的文件格式改成了DAT格式,那可以先尝试用记事本打开,如果不行那么就要询问给您这个DAT文件的人了。
你试下如果改成JAVA文件后缀 用编码utf-8 试试
⑤ JAVA中怎么读取DAT文件中的内容
DAT估计是个二进制或者文本跟普通读取文件是一样的
读取上来你再对文件格式进行拆分首先你要了解它的格式是什么你可以用
NOTEPAD++或者C32ASM打开看看
publicclassReadFromFile{
/**
*以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*/
(StringfileName){
Filefile=newFile(fileName);
InputStreamin=null;
try{
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
//一次读一个字节
in=newFileInputStream(file);
inttempbyte;
while((tempbyte=in.read())!=-1){
System.out.write(tempbyte);
}
in.close();
}catch(IOExceptione){
e.printStackTrace();
return;
}
try{
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
//一次读多个字节
byte[]tempbytes=newbyte[100];
intbyteread=0;
in=newFileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
//读入多个字节到字节数组中,byteread为一次读入的字节数
while((byteread=in.read(tempbytes))!=-1){
System.out.write(tempbytes,0,byteread);
}
}catch(Exceptione1){
e1.printStackTrace();
}finally{
if(in!=null){
try{
in.close();
}catch(IOExceptione1){
}
}
}
}
/**
*以字符为单位读取文件,常用于读文本,数字等类型的文件
*/
(StringfileName){
Filefile=newFile(fileName);
Readerreader=null;
try{
System.out.println("以字符为单位读取文件内容,一次读一个字节:");
//一次读一个字符
reader=newInputStreamReader(newFileInputStream(file));
inttempchar;
while((tempchar=reader.read())!=-1){
//对于windows下, 这两个字符在一起时,表示一个换行。
//但如果这两个字符分开显示时,会换两次行。
//因此,屏蔽掉 ,或者屏蔽 。否则,将会多出很多空行。
if(((char)tempchar)!=' '){
System.out.print((char)tempchar);
}
}
reader.close();
}catch(Exceptione){
e.printStackTrace();
}
try{
System.out.println("以字符为单位读取文件内容,一次读多个字节:");
//一次读多个字符
char[]tempchars=newchar[30];
intcharread=0;
reader=newInputStreamReader(newFileInputStream(fileName));
//读入多个字符到字符数组中,charread为一次读取字符数
while((charread=reader.read(tempchars))!=-1){
//同样屏蔽掉 不显示
if((charread==tempchars.length)
&&(tempchars[tempchars.length-1]!=' ')){
System.out.print(tempchars);
}else{
for(inti=0;i<charread;i++){
if(tempchars[i]==' '){
continue;
}else{
System.out.print(tempchars[i]);
}
}
}
}
}catch(Exceptione1){
e1.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOExceptione1){
}
}
}
}
/**
*以行为单位读取文件,常用于读面向行的格式化文件
*/
(StringfileName){
Filefile=newFile(fileName);
BufferedReaderreader=null;
try{
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader=newBufferedReader(newFileReader(file));
StringtempString=null;
intline=1;
//一次读入一行,直到读入null为文件结束
while((tempString=reader.readLine())!=null){
//显示行号
System.out.println("line"+line+":"+tempString);
line++;
}
reader.close();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOExceptione1){
}
}
}
}
/**
*随机读取文件内容
*/
(StringfileName){
RandomAccessFilerandomFile=null;
try{
System.out.println("随机读取一段文件内容:");
//打开一个随机访问文件流,按只读方式
randomFile=newRandomAccessFile(fileName,"r");
//文件长度,字节数
longfileLength=randomFile.length();
//读文件的起始位置
intbeginIndex=(fileLength>4)?4:0;
//将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[]bytes=newbyte[10];
intbyteread=0;
//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
//将一次读取的字节数赋给byteread
while((byteread=randomFile.read(bytes))!=-1){
System.out.write(bytes,0,byteread);
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(randomFile!=null){
try{
randomFile.close();
}catch(IOExceptione1){
}
}
}
}
/**
*显示输入流中还剩的字节数
*/
(InputStreamin){
try{
System.out.println("当前字节输入流中的字节数为:"+in.available());
}catch(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
StringfileName="C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}
⑥ 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;
}
⑦ 现在有一个dat文件是以二进制存储的。里面的内容格式我现在知道。我想用java来解析出来。
换UTF-8试试。
要不就是你解析有误。dat可能不是一个文本文件。
你这种读byte的方法恐怕有误
⑧ java解析dat文件内的内容,下面是dat里的信息
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileReader;
publicclassDatDemo{
publicstaticvoidmain(String[]args)throwsException{
Filefile=newFile("c:\test.dat");
BufferedReaderbr=newBufferedReader(newFileReader(file));//输入流
Stringstr;
while((str=br.readLine())!=null){//按行读取
String[]ss=str.trim().split(":");
if(ss.length==2){//如果包含key和value就输出
System.out.println("key="+ss[0].trim()+" value="+ss[1].trim());
}else{//只有key没有value就输出value=null
System.out.println("key="+ss[0].trim()+" value="+"null");
}
}
br.close();//关闭流
}
}
运行测试
key=ID
value=324,803
key=DocTitle
value=15/09/25-certifytrue
key=FileName
value=bakersam8.TIF
..........
....
.
⑨ 请问如何写一个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文件
可以通过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流会一直存在,直到程序运行结束。