Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How java splits a document according to character length given a fixed length

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how java splits documents according to character length given a fixed length. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Given fixed length (array aa [])

Split the document according to the character length (non-byte length)

Finally, output the document with spaces removed to ^ splice.

The first 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 input file path

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);   //没有则创建输出的文件   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 = "";   // 循环要获取的长度   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缓存区间;   StringBuffer buff = new StringBuffer();   char c;   int len = 0;   // 遍历String的每一个Char字符,计算当前总长度   // 如果到当前Char的的字节长度大于要截取的字符总长度,则跳出循环返回截取的字符串。   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)

Break

}

/ / return the final intercepted character result

/ / create a String object and pass in the target char Buff object

Return new String (buff)

}

}

test

Package work

Public class Test {

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

Int [] aa = {32 pencils 20 pas 2 pas 8 pr 5pr 100 15 pr 1pr 1pr 1pr 40j1}

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

}

}

Thank you for reading! This is the end of this article on "how java divides documents with fixed length according to character length". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report