溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java如何給定固定長度根據(jù)字符長分割文檔

發(fā)布時間:2021-11-20 13:48:05 來源:億速云 閱讀:165 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)java如何給定固定長度根據(jù)字符長分割文檔的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

  給定固定長度(數(shù)組 aa[ ])

  根據(jù)字符長(非字節(jié)長度)分割文檔

  最后輸出去除空格以 ^ 拼接的文檔

  第一個class

  package work;

  import java.io.BufferedReader;

  import java.io.BufferedWriter;

  import java.io.File;

  import java.io.FileInputStream;

  import java.io.FileWriter;

  import java.io.IOException;

  import java.io.InputStream;

  import java.io.InputStreamReader;

  import java.nio.charset.Charset;

  public class T1 {

  // inputFilePath 輸入文件路徑

  public static void parseCunegFile(String inputFilePath,String outputFilePath, int[] aa) throws Exception {

  BufferedWriter bw = null;

  BufferedReader br = null;

  int[] bb = new int[aa.length];

  bb[0] = 0;

  for (int i = 1; i < bb.length; i++) {

  bb[i] = bb[i - 1] + aa[i - 1]+1;

  aa[i]=aa[i]-1;

  }

  try {

  //讀取輸入的文件

  File ifile = new File(inputFilePath);

  InputStream is = new FileInputStream(ifile);

  //沒有則創(chuàng)建輸出的文件

  File ofile = new File(outputFilePath);

  //FileOutputStream out=new FileOutputStream(ofile);

  if(!ofile.exists()){

  ofile.createNewFile();

  }

  bw=new BufferedWriter(new FileWriter(ofile,true));

  br = new BufferedReader(new InputStreamReader(is, Charset.forName("gbk")));

  String line = "";

  // 讀取每一行

  while ((line = br.readLine()) != null) {

  String str = "";

  // 循環(huán)要獲取的長度

  for (int i = 0; i < aa.length; i++) {

  str = str.trim()+T2.substringByte(line, bb[i], aa[i]).trim()+"^";

  }鄭州人流多少錢 http://www.hnmt120.com/

  str = str.substring(0,str.length()-1);

  bw.write(str+"\n");

  }

  } catch (IOException e) {

  e.printStackTrace();

  }finally {

  br.close();

  bw.close();

  }

  }

  }

  第二個class

  package work;

  public class T2 {

  // orignal:需要截取的每一行 start:從第幾位開始 count:截取幾位

  public static String substringByte(String orignal, int start, int count) throws Exception {

  // 目標char Pull buff緩存區(qū)間;

  StringBuffer buff = new StringBuffer();

  char c;

  int len = 0;

  // 遍歷String的每一個Char字符,計算當前總長度

  // 如果到當前Char的的字節(jié)長度大于要截取的字符總長度,則跳出循環(huán)返回截取的字符串。

  for (int i = 0; i < orignal.toCharArray().length; i++) {

  c = orignal.charAt(i);

  len += String.valueOf(c).getBytes("GBK").length;

  if (len >= start && len <= start + count) {

  buff.append(c);

  }

  if (len > start + count)

  break;

  }

  // 返回最終截取的字符結(jié)果;

  // 創(chuàng)建String對象,傳入目標char Buff對象

  return new String(buff);

  }

  }

  測試

  package work;

  public class Test {

  public static void main(String[] args) throws Exception {

  int[] aa = {32,20,2,8,5,100,15,1,1,1,1,40,1};

  T1.parseCunegFile("C:\\Users\\Administrator\\Desktop\\1.txt","C:\\Users\\Administrator\\Desktop\\2.txt", aa);

  }

  }

感謝各位的閱讀!關(guān)于“java如何給定固定長度根據(jù)字符長分割文檔”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI