導航:首頁 > 文件教程 > java文件分割

java文件分割

發布時間:2025-02-16 10:43:12

A. 怎樣用java流來分割一個mp3文件代碼

package xuan;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.Buffer;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
public class mp3 {
public static void cutMusic() throws IOException{
File file=new File("E:\\薛之謙 - 那是你離開了北京的生活.flac");
File file2=new File("E:\\music");
FileInputStream fis =new FileInputStream(file);
FileOutputStream fos=null;
//if(file2.exists()!=true) {
// file2.mkdirs();
//}

int len=0;
int x=0;
int y=1020*1024;
byte [] one=new byte[y];
if(file.length()%y!=0) {
x=(int)(file.length()/y+1);
}else if(file.length()%y==0) {
x=(int)(file.length()/y);
}
for(int i=1;i<=x;i++) {
len=fis.read(one);
fos=new FileOutputStream (new File(file2,i+".flac"));
fos.write(one,0,len);
}
fis.close();
fos.close();
}
public static void mergeMusic()throws IOException{
File file=new File("E:\\merge.flac");
File file2=new File("E:\\music");
// if(file.exists()!=true) {
// file.createNewFile();
// }
File[]f=file2.listFiles();
FileInputStream fis=null;
FileOutputStream fos=new FileOutputStream(file);
BufferedOutputStream bos =new BufferedOutputStream(fos,1024*1024);
int len=0;
for(int i=0;i<f.length;i++) {
fis =new FileInputStream(f[i]);
BufferedInputStream bis =new BufferedInputStream(fis,1024*1024);
while((len=bis.read())!=-1) {
bos.write(len);
}
fos.flush();
fis.close();
}
bos.close();
fos.close();
}
public static void main(String[] args) throws IOException{
cutMusic();
mergeMusic();
// TODO Auto-generated method stub
}
}

B. 如何用Java分割大txt文件

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class FileCutter { /** * *sourceFile:源文件的路徑 *targetDirectory:保存文件的目錄(例:'C:\\') *prefix:是分割後文件的前綴(例:'2015-09-09') *size:是分隔後單一文件的大小單位是2kb的倍數,size傳10,分割後單一文件就是20K。傳100,文件就是2M一個。 * **/ public static void cutToMoreFile(String sourceFile, String targetDirectory, String prefix, int size) { //載入源文件 File source = new File(sourceFile); InputStream in = null; OutputStream out = null; int len = 0; int fileIndex = 1; //設置一次載入的大小 byte[] buffer = new byte[2048]; try { //把源文件讀到InputStream中 in = new FileInputStream(source); //循環 while(true) { //分割後的文件流 out = new FileOutputStream(targetDirectory + File.separator + prefix + fileIndex++ + ".txt"); for(int i = 0; i < size; i++) { //如果文件讀取完就退回方法。 if((len = in.read(buffer)) != -1) { //寫入分割後的文件 out.write(buffer, 0, len); }else { //執行finally內容後,退出方法 return; } } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { //關系流 in.close(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

C. java文本分割並存入數組

把所有的字元看做一個字元串,以#截取,保存到數組1中,再遍歷數組1,以/截取,再保存到子數組
private static String string = "文件名.txt 關鍵字/詞性/權重/出現個數#關鍵字/詞性/權重/出現個數#文件名.txt 關鍵字/詞性/權重/出現個數#關鍵字/詞性/權重/出現個數#文件名.txt 關鍵字/詞性/權重/出現個數#關鍵字/詞性/權重/出現個數#";
public static void main(String[] args) {
String[] strings = string.split("#");
for (int i = 0; i < strings.length; i++) {
String[] strings2 = strings[i].split("/");
for (int j = 0; j < strings2.length; j++) {
//在這里做保存的操作
System.out.print(strings2[j] + " ");
}
}
}

D. java 多線程 分割文件

在分割文件部分:
文件長度S,線程N。每塊D=S/N;
線程1 上傳范圍回 0....D
線程2 上傳范圍 D+1....2*D
線程3 上傳范圍 2*D+1....3D
線程N 上傳范圍 (N-1)*D+1....N*D
某一答個上傳完,再找其他線程未下載的范圍對半分..

E. java簡單問題,分割字元串

s.split(".")這個操作可以將字元串按照點進行分割,返回一個包含分割後字元串的數組。例如,如果有一個字元串"example.string.split",使用s.split(".")後,會得到一個包含"example"和"string.split"的數組。這在處理包含點分隔符的數據時非常有用,比如在解析URL或者郵件地址時。

對於逗號分隔的字元串,比如從CSV文件中讀取的數據,可以使用split(",")來分割。例如,"value1,value2,value3,value4"會被分割成一個包含"value1"、"value2"、"value3"、"value4"的數組。這樣就可以方便地逐個讀取每個欄位,而不必處理逗號。

有時候,你可能需要從一個文件名中截取文件名而不包含文件擴展名。例如,如果有一個文件名"123.jpg",你可能想要得到"123"。可以使用字元串的substring方法來實現。例如,String a = "123.jpg"; String after = a.substring(0,a.length()-4); 這段代碼會返回"123"。這里是從字元串的開頭截取到倒數第四個字元,即最後的".jpg"之前的字元。

在一些情況下,可能不知道具體的位置。這時可以使用indexOf方法來查找特定字元的位置。例如,int index = a.indexOf(".")可以得到"."字元第一次出現的位置。然後你可以使用substring方法從起始位置截取到該位置,或從該位置截取到字元串結束。

總的來說,split、substring和indexOf是處理字元串分割和截取的常用方法。它們可以讓你更靈活地處理和操作字元串數據。

使用這些方法時,需要注意邊界情況。例如,如果字元串中不存在特定的分隔符,或者字元串為空,那麼split方法會返回一個空數組。同時,substring方法需要確保起始位置小於結束位置,否則會拋出異常。

通過這些方法,你可以更高效地處理和解析字元串數據,從而更好地滿足不同的需求。

閱讀全文

與java文件分割相關的資料

熱點內容
iphone4ios71降級613 瀏覽:650
garageband升級 瀏覽:689
科學linux 瀏覽:447
哪裡的app理財好 瀏覽:799
linux下線程的創建線程數 瀏覽:804
怎麼改變文件大小 瀏覽:574
飛雪流水軟體注冊破解工具 瀏覽:814
csgo比賽數據哪裡看 瀏覽:961
已越獄iphone4升級ios7 瀏覽:607
微信分享但不是微商 瀏覽:877
git子模塊如何獲取最新數據 瀏覽:446
java項目ppt模板下載不了 瀏覽:493
不常用的app怎麼取消 瀏覽:230
js多點滑動選取范圍 瀏覽:756
環境監測app怎麼下載 瀏覽:444
美創科技外數據安全領域怎麼樣 瀏覽:143
蘋果手機上怎麼修改文件表 瀏覽:865
網上如何年審營業執照網站 瀏覽:304
電信版iphone好嗎 瀏覽:469
瀟湘冬兒在哪個網站 瀏覽:838

友情鏈接