import java.io.File;
创新互联是一家集网站建设,徐闻企业网站建设,徐闻品牌网站建设,网站定制,徐闻网站建设报价,网络营销,网络优化,徐闻网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
public class Test {
public static void main(String[] args) throws Exception {
String searchStr = "APPTYPEID";
File searchDir = new File("D:\\xampp\\htdocs\\discuz");
ListSearch list = new ArrayListSearch();
CollectionFile files = FileUtils.listFiles(searchDir, null, true);
ListString lines = null;
for (File file : files) {
lines = FileUtils.readLines(file);
for (int i = 0; i lines.size(); i++) {
if (lines.get(i).indexOf(searchStr) != -1) {
Search s = new Search();
s.setFilename(file.getName());
s.setLine(i + 1);
s.setContent(lines.get(i));
list.add(s);
}
}
}
StringBuffer sb = new StringBuffer();
for (Search s : list) {
sb.append(s.getFilename() + " , " + s.getLine() + " , " + s.getContent()).append(
IOUtils.LINE_SEPARATOR);
}
FileUtils.writeStringToFile(new File("D:\\search.txt"), sb.toString(), "UTF-8");
}
}
class Search {
private String filename;
private int line;
private String content;
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public void setLine(int line) {
this.line = line;
}
public int getLine() {
return line;
}
}
File
f=
new
File("你的文件路径");
system.out.println(f.length());
以上就是通过length获取文件的字节数。
android中的java获取文件大小的方法:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
public class FileContent {
private String path = "F:\\下载说明.txt";
public FileContent() throws IOException
{
File f = new File(path);
FileReader fileReader = new FileReader(f);
BufferedReader br = new BufferedReader(fileReader);
String str;
while((str = br.readLine() ) != null)
{
System.out.println(str);
}
System.out.println(new FileInputStream(new File(path)).available() / 1024 / 1024 +"M");
}
public static void main(String[] args) {
try {
new FileContent();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
File file = new File("D:/201709201336160.05V");
getFileSize(file);
}
/**
* 获取文件大小
* @param file
*/
public static void getFileSize(File file) {
FileInputStream fis = null;
try {
if(file.exists() file.isFile()){
String fileName = file.getName();
fis = new FileInputStream(file);
System.out.println("文件"+fileName+"的大小是:"+fis.available()+"\n");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(null!=fis){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这个可以判断大小 输出的是byte 你转化一下MB就行了