導航:首頁 > 文件目錄 > java掃描java文件內容嗎

java掃描java文件內容嗎

發布時間:2023-05-23 10:55:06

『壹』 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實現定時掃描文件夾

  1. 寫一個類繼承Thread,重寫run方法,在辯局run方法裡面寫你要做的處理,然後根據你的定時要枝賣求來設置sleep的時間。

    newThread(){
    run(){
    //根據具體需求設置時間:毫秒
    sleep(100ms);
    while(true){
    //掃描文件夾處理
    猛灶逗....
    //根據具體需求設置時間:毫秒
    sleep(24*3600*1000);
    }
    }
    }
  2. java中不是有定時器嗎?寫好自己的任務,定時執行就可以了

    Timertimer=newTimer();
    Tasktask=newTask(){
    publicvoidrun(){
    System.out.println("running...");
    }
    };
    timer.shele(task,delayDate,internalTime);
  3. 這種跟操作系統關系緊密的操作沒必要用java來做,除非你是J2EE,那麼Spring里有quataz類似Unix裡面cronjob的開源框架可以用,很方便

  4. 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;
}

閱讀全文

與java掃描java文件內容嗎相關的資料

熱點內容
血緣詛咒怎麼升級 瀏覽:604
文件分享到微信怎麼操作 瀏覽:393
tmx文件trados 瀏覽:704
大數據與會計選課怎麼選 瀏覽:684
網路的不穩定因素有哪些 瀏覽:950
獵豹瀏覽器javascript 瀏覽:723
哪個列印軟體保存數據時間長 瀏覽:873
ps合並後源文件 瀏覽:74
無線怎麼設置沒有網路 瀏覽:548
網站怎麼換模板 瀏覽:511
ps處理後的文件列印出來很模糊 瀏覽:204
有什麼看污污動漫的網站 瀏覽:184
用戶名郵箱手機號資料庫 瀏覽:879
cad不能啟動此對象的源應用程序 瀏覽:901
微信上的騙子都怎麼騙人 瀏覽:294
加工中心編程如何算重量 瀏覽:758
什麼是機靈數據 瀏覽:724
ecshop配置文件 瀏覽:116
excel兩個表格怎麼對比相同數據 瀏覽:383
ps4港服文件怎麼弄 瀏覽:560

友情鏈接