Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 305|回复: 0

[JavaIO学习]nio读写文件实例

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-29 23:57:32 | 显示全部楼层 |阅读模式
    一、读写工具
    1. import java.io.FileInputStream;   
    2. import java.io.FileOutputStream;   
    3. import java.io.IOException;   
    4. import java.nio.ByteBuffer;   
    5. import java.nio.MappedByteBuffer;   
    6. import java.nio.channels.FileChannel;   
    7.   
    8.   
    9.   
    10. /** *//**  
    11. * @author lichun  
    12. *  
    13. * TODO To change the template for this generated type comment go to  
    14. * Window - Preferences - Java - Code Style - Code Templates  
    15. */  
    16. public class FileUtil {   
    17.       
    18.     public static ByteBuffer readFile(String filename) throws Exception   
    19.     {   
    20.         FileChannel fiChannel = new FileInputStream(filename).getChannel();   
    21.         MappedByteBuffer mBuf;   
    22.         mBuf = fiChannel.map(FileChannel.MapMode.READ_ONLY, 0, fiChannel.size());   
    23.         fiChannel.close();   
    24.         fiChannel = null;   
    25.            
    26.         return mBuf;         
    27.            
    28.     }   
    29.       
    30.       
    31.     public static void saveFile(ByteBuffer src, String filename) throws Exception   
    32.     {   
    33.         FileChannel foChannel = new FileOutputStream(filename).getChannel();   
    34.         foChannel.write(src);   
    35.         foChannel.close();     
    36.         foChannel = null;   
    37.     }   
    38.       
    39.     public static void saveFile(FileChannel fiChannel, String filename) throws IOException   
    40.     {   
    41.         MappedByteBuffer mBuf;   
    42.         mBuf = fiChannel.map(FileChannel.MapMode.READ_ONLY, 0, fiChannel.size());   
    43.   
    44.         FileChannel foChannel = new FileOutputStream(filename).getChannel();   
    45.         foChannel.write(mBuf);   
    46.            
    47.         fiChannel.close();   
    48.         foChannel.close();   
    49.            
    50.         fiChannel = null;   
    51.         foChannel = null;   
    52.     }   
    53.       
    54.   
    55.     public static void main(String[] args) throws Exception   
    56.     {        
    57.         final String suffix = ".txt";   
    58.         final String filename = "bufferTemp";   
    59.         final String srcFile =  filename + suffix ;   
    60.         final String backupFile = filename + "-" + System.currentTimeMillis() + suffix;   
    61.         ByteBuffer byteBuffer = FileUtil.readFile(srcFile);   
    62.         FileUtil.saveFile(byteBuffer, backupFile);   
    63.         byteBuffer = null;   
    64.            
    65.     }   
    66. }  
    67. 二、写文件线程

    68. import java.nio.channels.FileChannel;   
    69.   
    70. /** *//**  
    71. * @author lichun  
    72. *  
    73. * TODO To change the template for this generated type comment go to  
    74. * Window - Preferences - Java - Code Style - Code Templates  
    75. */  
    76. public class WriteFileThread extends Thread {   
    77.   
    78.     private FileChannel fiChannel;   
    79.     private String fileName;   
    80.       
    81.     public WriteFileThread(String name)   
    82.     {   
    83.         super(name);   
    84.     }   
    85.       
    86.     public WriteFileThread(String name, FileChannel fiChannel, String fileName)   
    87.     {   
    88.         this(name);   
    89.         this.fiChannel = fiChannel;   
    90.         this.fileName = fileName;   
    91.     }   
    92.       
    93.     public void setFiChannel(FileChannel fiChannel)   
    94.     {   
    95.         this.fiChannel = fiChannel;   
    96.     }   
    97.       
    98.     public FileChannel getFiChannel() {   
    99.         return fiChannel;   
    100.     }   
    101.      
    102.     public void setFileName(String fileName) {   
    103.         this.fileName = fileName;   
    104.     }   
    105.       
    106.     public String getFileName() {   
    107.         return fileName;   
    108.     }   
    109.       
    110.     public void run()   
    111.     {   
    112.         System.out.println("in Thread: " + this.getName());   
    113.         try {   
    114.             FileUtil.saveFile(this.fiChannel, this.fileName);   
    115.         } catch (Exception e) {   
    116.             System.out.println("The file is not founded: " + fileName);   
    117.             e.printStackTrace();   
    118.         }   
    119.         System.out.println("end Thread: " + this.getName());   
    120.     }   
    121. }  
    122. 三、复制文件内容生成新文件

    123. import java.io.FileInputStream;   
    124. import java.io.FileNotFoundException;   
    125. import java.nio.ByteBuffer;   
    126. import java.nio.channels.FileChannel;   
    127. import java.util.*;   
    128.   
    129.   
    130. /** *//**  
    131. * @author lichun  
    132. *  
    133. * TODO To change the template for this generated type comment go to  
    134. * Window - Preferences - Java - Code Style - Code Templates  
    135. */  
    136. public class TransferFile {   
    137.       
    138.     private String srcFile;   
    139.       
    140.     List files = new ArrayList();   
    141.     ByteBuffer byteBuffer;   
    142.       
    143.     public TransferFile()   
    144.     {   
    145.         createFileNames();   
    146.     }   
    147.       
    148.     private void createFileNames()   
    149.     {   
    150.         for (int i = 0; i < 10; i++)   
    151.         {   
    152.             files.add(i + "");   
    153.         }   
    154.     }     
    155.       
    156.     public List getFiles()   
    157.     {   
    158.         return this.files;   
    159.     }   
    160.   
    161.     public String getSrcFile() {   
    162.         return srcFile;   
    163.     }   
    164.   
    165.     public void setSrcFile(String srcFile) {   
    166.         this.srcFile = srcFile;   
    167.     }   
    168.       
    169.     public void saveFiles() throws FileNotFoundException   
    170.     {   
    171.         String tempFile;   
    172.         for (int i = 0; i < this.files.size(); i++)   
    173.         {   
    174.             tempFile = "test-files/" + (String)files.get(i) + ".txt";   
    175.             System.out.println("begin create thread for " + tempFile);   
    176.                
    177.             FileChannel fiChannel = new FileInputStream(this.srcFile).getChannel();   
    178.                
    179.             WriteFileThread writeFileThread = new WriteFileThread("writeFile"+i, fiChannel, tempFile);   
    180.             writeFileThread.start();      
    181.         }   
    182.            
    183.     }   
    184.   
    185.     public static void main(String[] args) throws Exception   
    186.     {   
    187.         final String srcFile = "test-files/transferFile.txt";   
    188.         TransferFile transferFile = new TransferFile();   
    189.         transferFile.setSrcFile(srcFile);   
    190.         transferFile.saveFiles();           
    191.     }   
    192. }  
    复制代码


       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/10/29/235732093.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 23:20 , Processed in 0.338522 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表