求java高手,java读取txt中内容,按内容拆分成多个txt,如下图,拆分成两个txt文件

求java高手,java读取txt中内容,按内容拆分成多个txt,如下图,拆分成两个txt文件,第1张

没有定拆分规则   以下代码是前5个行为一个txt   其余的为另外一个txt

package com.kidd.baiduzhidao

import java.io.File

import java.io.FileNotFoundException

import java.io.FileWriter

import java.io.IOException

import java.util.Scanner

public class baiduzhidao {

    public static void main(String[] args) {

        File file = new File("D:"+File.separator+"t1.txt")

        String[] ss = readTxt(file)

        

        if(ss==null || ss.length==0){

            System.out.println("读取txt内容发生错误,txt内容可能为空.")

            return 

        }

        file = new File("D:"+File.separator+"1.txt")

     银大   

        if(!writeTxt(file, ss ,0, 5)){

            System.out.println("写入第一个txt发生了错误.终止")

            return 

        }

        

        file = new File("D:"+File.separator+"2.txt")

        if(!writeTxt(file, ss ,5)){

            System.out.println("写入第二个txt发生了错误.终止")

            return 

        }

        

    }

    /**

     * 写出数组内容从start开始的所有内容到txt文件

     * @param file 待写出的txt文件的File形式

     * @param ss 数组对象

     * @param start 开始位置

     * @return 成功返回true  发生错误返回false

     */

    private static boolean writeTxt(File file, String[] ss, int start) {

        return writeTxt(file,ss,start,ss.length-start)

    }

    /**

     * 写出数组内容从start位置开始共写出length个  到txt文件

     * @param file 待写出的txt文件的File形式

     * @param ss  数组对象

     * @param start 开始位置

     * @param length  写出长度

     * @return  成功返回true  发生错误返回false

     */

    private static boolean writeTxt(File file, String[] ss,int start, int length) {

        FileWriter fWriter

        try {

            fWriter = new FileWriter(file, false)

        } catch (IOException e) {

            e.printStackTrace()

            return false 

        }

        

        try {

        for(int i = start , k = ss.length  i < k && i < start+length  i++){

                fWriter.write(ss[i]+"\r\n")

        }

        

        } catch (IOException e) {

            e.printStackTrace()

            if(!closeFileWriter(fWriter)){

              袭脊  fWriter = null 

            }

            return false 

        }

        if(!closeFileWriter(fWriter)){

            fWriter = null 

        }

        

        return true 

    }

    /**

     * 关闭文件流

     * @param fWriter

     * @return  成功关闭返回true,发生错误返回false

     */

    private static boolean closeFileWriter(FileWriter fWriter) {

        if(fWriter==null){

            return false

        锋禅竖}

        try {

            fWriter.close()

        } catch (IOException e) {

            e.printStackTrace()

        }

        return true 

    }

    /**

     * 读取txt文件内容,返回该文本内容的String[]形式

     * @param file txt文件的File形式

     * @return  该文本内容的String[]形式  若读取发生错误  则返回null

     */

    private static String[] readTxt(File file) {

        Scanner scanner

        try {

            scanner = new Scanner(file)

        } catch (FileNotFoundException e) {

            e.printStackTrace()

            return null

        }

        String string = ""

        /**

         * 读取到的每一行用,分割

         */

        while (scanner.hasNext()) {

            string += scanner.nextLine() + ","

        }

        scanner.close()

        

        return string.split(",")

    }

}

以下一个拆分txt的Util类

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()

   }

  }

 }

}

以上,详细请看注释

package demo

import java.io.BufferedInputStream

import java.io.BufferedReader

import java.io.File

import java.io.FileInputStream

import java.io.InputStreamReader

import java.util.ArrayList

import java.util.List

public class DemoApp {

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

// 指定的文件

File file = new File("E:\\Workspaces\\eclipse3.7\\Demo\\src\\test.txt")

// 装载毁郑握list

List<String> list = new ArrayList<String>()

// 读取文件

FileInputStream fis = new FileInputStream(file)

BufferedReader 纤庆br = new BufferedReader(new InputStreamReader(fis))

StringBuffer buffer = new StringBuffer()

String line

int i = 0

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

System.out.println(line)

if (i == 0) {

buffer.append(line + "\n")

} else {

// 判丛汪断截取点

if (line.substring(0, 1).equals(">")) {

list.add(buffer.toString())

buffer = new StringBuffer()

buffer.append(line + "\n")

} else {

buffer.append(line + "\n")

}

}

i++

}

if (line == null) {

list.add(buffer.toString())

}

// test

System.out.println("--------------------------")

for(int j=0 j<list.size() j++) {

System.out.println( j + ":   " + list.get(j))

System.out.println("-----------------------")

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12164338.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存