『壹』 java如何扫描一个文本然后输出这个文本里的一些内容
通过IO读取文件 FileInputStream fis=new FileInputStream("要读取的文件要路径")
byte[]buf=new byte[1024];设置基于缓存的读写方法
int i=-1;
while((i=fis.read())!=-1){
system.out.println((char)i)}将文件内容输入到控制台 这里的文件特指文本文件如果是别的话可能导致乱码
『贰』 用java写一段程序扫描文件夹下所有后缀为.txt的文件代码
链接:
提取码:9a2f复制这段内容后打开网络网盘手机App,操作更方便哦
作品简介:
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。
『叁』 如何用Java实现扫描本地文件夹中所有文本文件并判断其内容,内容超过10行就分割分割的内容放入新文件
用Scanner读腊孙取文件,用它的nextLine方法读取下一行,当超过10行以后,每轮孝链十行绑在一起用FileWriter写入一个新文件
------------------------------------------------------------------------------
是在内存中处理的啊慎戚..至于你要时间限制就不好说了...毕竟CPU的处理能力有别..
『肆』 用 java 做一个程序扫描指定的目录及其所有的子目录和文件,输出格式要求如下(假设扫描C:) C:\ |--test
public static void main(String[] args) {
// TODO Auto-generated method stub
int j = 0;
while(j<5) {
File f = new File("c:/Mailboxes/in/");
File[] fArray = f.listFiles();
for(int i=0; i<fArray.length; i++) {
if(fArray[i].getName().matches("[\\w]*.EML$")) {
System.out.println(fArray[i].getName());
try {
FileInputStream is = new FileInputStream(fArray[i]);
byte[] b = new byte[(int)fArray[i].length()];
is.read(b);
FileOutputStream os = new FileOutputStream("森岁c:/mail/" + fArray[i].getName());
os.write(b);
os.close();
is.close();
fArray[i].delete();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
/让春升/坦老 TODO Auto-generated catch block
e.printStackTrace();
}
j++;
}
}
『伍』 如何用java或者c#写个扫描整个文件夹及子文件夹,验证多个文件的md5值,并将md5值重复但不同
java的,我已经测试可以使用。
但是单个文件很大的话可能算md5会比较慢
importjava.security.MessageDigest;
importjava.security.NoSuchAlgorithmException;
importjava.util.ArrayList;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.math.BigInteger;
importjava.nio.MappedByteBuffer;
importjava.nio.channels.FileChannel;
publicclassMain
{
ArrayList<String>md5List=newArrayList<String>();
publicStringgetMd5(Filefile){
Stringvalue=null;
FileInputStreamin=null;
try{
in=newFileInputStream(file);
MappedByteBufferbyteBuffer=in.getChannel().map(FileChannel.MapMode.READ_ONLY,0,file.length());
MessageDigestmd5=MessageDigest.getInstance("MD5");
md5.update(byteBuffer);
BigIntegerbi=newBigInteger(1,md5.digest());
value=bi.toString(16);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(NoSuchAlgorithmExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(null!=in){
try{
in.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
returnvalue;
}
publicvoidrun(Filefile){
if(file!=null){
if(file.isDirectory()){
Filef[]=file.listFiles();
if(f!=null){
for(inti=0;i<f.length;i++)
run(f[i]);
}
}else{
Stringmd5=getMd5(file);
if(md5List.contains(md5)){
System.out.println(file.toString());
file.delete();
}else{
md5List.add(md5);
}
}
}
}
publicstaticvoidmain(Stringargs[]){
Mainm=newMain();
Stringpath="D:\test";
m.run(newFile(path));
}
}
『陆』 Java怎么实现扫描多个目录中(包括子目录)的指定文件,并且删除它们
思路如下使用递归来自
public static void de(File f)
{ File [] b = f.listFiles();
//获取包含file对象对应的子目录或者文件
for(int i =0;i<b.length;i++}{
if(b[i].isFile()){
b[i].delete(); //判断是否为文件如果是 就删除 }
else{ de(b[i]);//否则重新递归到方法中 }
} f.delete();//最后删除该目录中所有文件后就删除该目录 }
『柒』 求帮忙用java编写一个扫描程序,只需实现能扫描出特征码库中的文件就行,多谢多谢。
import java.io.FileOutputStream;
import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.Node;
import org.htmlparser.Parser;
public class ParserHtml {
private static String ENCODE = "GBK";
/**
* 中文字符编码
*
*/
private String message(String codingMsg) {
String str = null;
try {
str = new String(codingMsg.getBytes(ENCODE), System.getProperty("file.encoding"));
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
/**
* 写入操作
* @param filePath 静态页面路径
* @param fileStr 网页内容
* @throws Exception
*/
public void writeSourceFile(String szFileName,String pageUrl) {
try {
String parserContent = readerPageByUrl(pageUrl);
File file=new File(szFileName);
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fileout = new FileOutputStream(file);
fileout.write(parserContent.getBytes());
fileout.close();
}
catch( Exception e ) {
e.printStackTrace();
}
}
/**
* 根据网址读取网页HTML内容
* @param szFileName 静态页面路径
*/
public String readerPageByUrl(String pageUrl) throws Exception{
StringBuffer parserContent=new StringBuffer();
String returnStr = "";
URL url = null;
try{
url = new URL(pageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
Parser parser = new Parser(connection);
for (NodeIterator i = parser.elements(); i.hasMoreNodes();) {
Node node = i.nextNode();
parserContent.append(node.toHtml(true)+"\n");
}
returnStr = message(parserContent.toString().trim());
System.out.println(returnStr);
}
catch( Exception e ) {
e.printStackTrace();
}
return returnStr;
}
public static void main(String[] args) {
String pageUrl="http://10.1.250.31:8080/login.jsp";
String filePath="E://static.txt";
try {
new ParserHtml().writeSourceFile(filePath, pageUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
『捌』 java编辑一个扫描文件的方法,要求可以扫描根目录下的所有文件
package com.sunjob;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
public class Js {
/**
* @param args
*/
//初始化n,用于计数
static int n=0;
public static void get(File file) {
try {
//判断文件是否是文件,如果是文件,获取路径,并计数
if(file.isFile())
{
n++;
System.out.println(file.getAbsolutePath());
}
else
{
//如果是文件夹,声明一个数组放文件夹和他的子文件
File[] f=file.listFiles();
//遍历文件散余指件冲配下的文件,并获取路径
for (File file2 : f) {
get(file2);
}
}
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
/毁岩/ TODO Auto-generated method stub
这是扫描c盘的所有文件,可以修改
File file=new File("c:\\");
get(file);
System.out.println("文件个数为:"+n);
}
}
『玖』 如何用java实现定时扫描文件夹
写一个类继承Thread,重写run方法,在辩局run方法里面写你要做的处理,然后根据你的定时要枝卖求来设置sleep的时间。
newThread(){
run(){
//根据具体需求设置时间:毫秒
sleep(100ms);
while(true){
//扫描文件夹处理
猛灶逗....
//根据具体需求设置时间:毫秒
sleep(24*3600*1000);
}
}
}
java中不是有定时器吗?写好自己的任务,定时执行就可以了
Timertimer=newTimer();
Tasktask=newTask(){
publicvoidrun(){
System.out.println("running...");
}
};
timer.shele(task,delayDate,internalTime);
这种跟操作系统关系紧密的操作没必要用java来做,除非你是J2EE,那么Spring里有quataz类似Unix里面cronjob的开源框架可以用,很方便
Unix、linux操作系统配到cronjob里即可,Windows操作系统的“开始”-》“附件”-》系统工具-》计划任务
『拾』 我想用JAVA实现扫描一篇文本文档,并判断文中是否含有预定的关键字,应该怎么实现
这个我写的一个编辑dhcp配置文件的方法,你可以参考下
public boolean editDHCP(DHCP dhcp) {
File file=new File("/etc/dhcpd.conf");
File tFile=new File("/etc/dhcpd_cp");
try {
BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tFile)));
String temp=null;
String str="";
while((temp=in.readLine())!=null){
if(temp.indexOf("subnet-mask")!=-1){
str="option subnet-mask "+dhcp.getSubnetmark()+";";
}else if (temp.indexOf("broadcast-address")!=-1) {
str="option broadcast-address "+ dhcp.getBroadcastadd()+";";
}else if (temp.indexOf("routers-0")!=-1) {
str="option routers-0 "+ dhcp.getRouter()+";";
}else if (temp.indexOf("domain-name-server-0")!=-1) {
str="option domain-name-server-0 "+dhcp.getDNS_1()+";";
}else if (temp.indexOf("domain-name-server-1")!=-1) {
str="option domain-name-server-0 "+dhcp.getDNS_2()+";";
}else if (temp.indexOf("subnet")!=-1) {
str="subnet "+dhcp.getSubnet()+" netmask "+dhcp.getSubnetmark()+"{";
}else if (temp.indexOf("range")!=-1) {
str="range "+dhcp.getRange_from()+" "+dhcp.getRange_to();
}else{
str=temp;
}
out.write(str);
out.write("\n");
}
out.flush();
out.close();
in.close();
file.renameTo(new File("/etc/dhcpd.conf_back"));
if(tFile.renameTo(new File("/etc/dhcpd.conf"))){
return true;
}else{
return false;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}