java IO流中,如何判断一个文本是不是空的?

如题。。
比如我用FileInputStream去读取一个文本,如果这个文本什么内容都没有,程序就不去读取。。。如果有内容则读取。。

import java.io.*;
public class test
{
    public static void main(String args[]) throws Exception{
        FileInputStream fin=new FileInputStream("F:\\sample.txt");
        byte[] by=new byte[1000];
        int size=fin.available();
        if(size==0){
           System.out.println("文件为空!!");
        }else{
           int len=fin.read(by);
           System.out.println(new String(by,0,len));
        }
    }
}

关键是这一句,这一句能判断文件是否为空

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-09-21
import java.io.*;
public class test
{
public static void main(String args[]) throws Exception{
FileInputStream fin=new FileInputStream("F:\\sample.txt");
byte[] by=new byte[1000];
int size=fin.available();
if(size==0){
System.out.println("文件为空!!");
}else{
int len=fin.read(by);
System.out.println(new String(by,0,len));
}
}
}
import java.io.*;
public class test
{
public static void main(String args[]) throws Exception{
FileInputStream fin=new FileInputStream("F:\\sample.txt");
byte[] by=new byte[1000];
int size=fin.available();
if(size==0){
System.out.println("文件为空!!");
}else{
int len=fin.read(by);
System.out.println(new String(by,0,len));
}
}
}
import java.io.*;
public class test
{
public static void main(String args[]) throws Exception{
FileInputStream fin=new FileInputStream("F:\\sample.txt");
byte[] by=new byte[1000];
int size=fin.available();
if(size==0){
System.out.println("文件为空!!");
}else{
int len=fin.read(by);
System.out.println(new String(by,0,len));
}
}
}
import java.io.*;
public class test
{
public static void main(String args[]) throws Exception{
FileInputStream fin=new FileInputStream("F:\\sample.txt");
byte[] by=new byte[1000];
int size=fin.available();
if(size==0){
System.out.println("文件为空!!");
}else{
int len=fin.read(by);
System.out.println(new String(by,0,len));
}
}
}
import java.io.*;
public class test
{
public static void main(String args[]) throws Exception{
FileInputStream fin=new FileInputStream("F:\\sample.txt");
byte[] by=new byte[1000];
int size=fin.available();
if(size==0){
System.out.println("文件为空!!");
}else{
int len=fin.read(by);
System.out.println(new String(by,0,len));
}
}
}
第2个回答  2013-05-05

用readline()方法

while(line=in.readLine()!==null)
{
// 如果不为空,你要进行的读取操作。
}

第3个回答  2013-05-05
File f=new File();

判断下f.length
第4个回答  2013-05-05
从流里面读取字节,判断是否为空就可以了。
相似回答