Java基础知识回顾-3 输入输出流

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:
 

代码风格:http://svn.alibaba-inc.com/repos/ali_platform/spec/codestyle/

RandomAccessFile:支持随机读取,随意在文件内跳转,操作灵活但需要计算跳转的位置等

写入:

 
  1. import java.io.File ; 
  2. import java.io.RandomAccessFile ; 
  3. public class RandomAccessFileDemo01{ 
  4.     // 所有的异常直接抛出,程序中不再进行处理 
  5.     public static void main(String args[]) throws Exception{ 
  6.         File f = new File("d:" + File.separator + "test.txt") ; // 指定要操作的文件 
  7.         RandomAccessFile rdf = null ;       // 声明RandomAccessFile类的对象 
  8.         rdf = new RandomAccessFile(f,"rw") ;// 读写模式,如果文件不存在,会自动创建 
  9.         String name = null ; 
  10.         int age = 0 ; 
  11.         name = "zhangsan" ;         // 字符串长度为8 
  12.         age = 30 ;                  // 数字的长度为4 
  13.         rdf.writeBytes(name) ;      // 将姓名写入文件之中 
  14.         rdf.writeInt(age) ;         // 将年龄写入文件之中 
  15.         name = "lisi    " ;         // 字符串长度为8 
  16.         age = 31 ;                  // 数字的长度为4 
  17.         rdf.writeBytes(name) ;      // 将姓名写入文件之中 
  18.         rdf.writeInt(age) ;         // 将年龄写入文件之中 
  19.         name = "wangwu  " ;         // 字符串长度为8 
  20.         age = 32 ;                  // 数字的长度为4 
  21.         rdf.writeBytes(name) ;      // 将姓名写入文件之中 
  22.         rdf.writeInt(age) ;         // 将年龄写入文件之中 
  23.         rdf.close() ;               // 关闭 
  24.     } 
  25. }; 

读取:

 
  1. import java.io.File ; 
  2. import java.io.RandomAccessFile ; 
  3. public class RandomAccessFileDemo02{ 
  4.     // 所有的异常直接抛出,程序中不再进行处理 
  5.     public static void main(String args[]) throws Exception{ 
  6.         File f = new File("d:" + File.separator + "test.txt") ; // 指定要操作的文件 
  7.         RandomAccessFile rdf = null ;       // 声明RandomAccessFile类的对象 
  8.         rdf = new RandomAccessFile(f,"r") ;// 以只读的方式打开文件 
  9.         String name = null ; 
  10.         int age = 0 ; 
  11.         byte b[] = new byte[8] ;    // 开辟byte数组 
  12.         // 读取第二个人的信息,意味着要空出第一个人的信息 
  13.         rdf.skipBytes(12) ;     // 跳过第一个人的信息 
  14.         for(int i=0;i<b.length;i++){ 
  15.             b[i] = rdf.readByte() ; // 读取一个字节 
  16.         } 
  17.         name = new String(b) ;  // 将读取出来的byte数组变为字符串 
  18.         age = rdf.readInt() ;   // 读取数字 
  19.         System.out.println("第二个人的信息 --> 姓名:" + name + ";年龄:" + age) ; 
  20.         // 读取第一个人的信息 
  21.         rdf.seek(0) ;   // 指针回到文件的开头 
  22.         for(int i=0;i<b.length;i++){ 
  23.             b[i] = rdf.readByte() ; // 读取一个字节 
  24.         } 
  25.         name = new String(b) ;  // 将读取出来的byte数组变为字符串 
  26.         age = rdf.readInt() ;   // 读取数字 
  27.         System.out.println("第一个人的信息 --> 姓名:" + name + ";年龄:" + age) ; 
  28.         rdf.skipBytes(12) ; // 空出第二个人的信息 
  29.         for(int i=0;i<b.length;i++){ 
  30.             b[i] = rdf.readByte() ; // 读取一个字节 
  31.         } 
  32.         name = new String(b) ;  // 将读取出来的byte数组变为字符串 
  33.         age = rdf.readInt() ;   // 读取数字 
  34.         System.out.println("第三个人的信息 --> 姓名:" + name + ";年龄:" + age) ; 
  35.         rdf.close() ;               // 关闭 
  36.     } 
  37. }; 

 

一、输入流

1、输入流, 从文件中读取内容到byte数组之中, 再截取byte数组中内容, 到结束字符为止

 
  1. import java.io.File ; 
  2. import java.io.InputStream ; 
  3. import java.io.FileInputStream ; 
  4. public class InputStreamDemo02{ 
  5.     public static void main(String args[]) throws Exception{    // 异常抛出,不处理 
  6.         // 第1步、使用File类找到一个文件 
  7.         File f= new File("d:" + File.separator + "test.txt") ;  // 声明File对象 
  8.         // 第2步、通过子类实例化父类对象 
  9.         InputStream input = null ;  // 准备好一个输入的对象 
  10.         input = new FileInputStream(f)  ;   // 通过对象多态性,进行实例化 
  11.         // 第3步、进行读操作 
  12.         byte b[] = new byte[1024] ;     // 所有的内容都读到此数组之中 
  13.         int len = input.read(b) ;       // 读取内容 
  14.         // 第4步、关闭输出流 
  15.         input.close() ;                     // 关闭输出流\ 
  16.         System.out.println("读入数据的长度:" + len) ; 
  17.         System.out.println("内容为:" + new String(b,0,len)) ;  // 把byte数组变为字符串输出 
  18.     } 
  19. }; 

2、数组流, 开辟文件内容大小的空间以便读入:byte b[] = new byte[(int)f.length()] ;

 
  1. import java.io.File ; 
  2. import java.io.InputStream ; 
  3. import java.io.FileInputStream ; 
  4. public class InputStreamDemo03{ 
  5.     public static void main(String args[]) throws Exception{    // 异常抛出,不处理 
  6.         // 第1步、使用File类找到一个文件 
  7.         File f= new File("d:" + File.separator + "test.txt") ;  // 声明File对象 
  8.         // 第2步、通过子类实例化父类对象 
  9.         InputStream input = null ;  // 准备好一个输入的对象 
  10.         input = new FileInputStream(f)  ;   // 通过对象多态性,进行实例化 
  11.         // 第3步、进行读操作 
  12.         byte b[] = new byte[(int)f.length()] ;      // 数组大小由文件决定 
  13.         int len = input.read(b) ;       // 读取内容 
  14.         // 第4步、关闭输出流 
  15.         input.close() ;                     // 关闭输出流\ 
  16.         System.out.println("读入数据的长度:" + len) ; 
  17.         System.out.println("内容为:" + new String(b)) ;    // 把byte数组变为字符串输出 
  18.     } 
  19. }; 

3、

 
  1. import java.io.File ; 
  2. import java.io.InputStream ; 
  3. import java.io.FileInputStream ; 
  4. public class InputStreamDemo04{ 
  5.     public static void main(String args[]) throws Exception{    // 异常抛出,不处理 
  6.         // 第1步、使用File类找到一个文件 
  7.         File f= new File("d:" + File.separator + "test.txt") ;  // 声明File对象 
  8.         // 第2步、通过子类实例化父类对象 
  9.         InputStream input = null ;  // 准备好一个输入的对象 
  10.         input = new FileInputStream(f)  ;   // 通过对象多态性,进行实例化 
  11.         // 第3步、进行读操作 
  12.         byte b[] = new byte[(int)f.length()] ;      // 数组大小由文件决定 
  13.         for(int i=0;i<b.length;i++){ 
  14.             b[i] = (byte)input.read() ;     // 读取内容 
  15.         } 
  16.         // 第4步、关闭输出流 
  17.         input.close() ;                     // 关闭输出流\ 
  18.         System.out.println("内容为:" + new String(b)) ;    // 把byte数组变为字符串输出 
  19.     } 
  20. }; 

4、

 
  1. import java.io.File ; 
  2. import java.io.InputStream ; 
  3. import java.io.FileInputStream ; 
  4. public class InputStreamDemo05{ 
  5.     public static void main(String args[]) throws Exception{    // 异常抛出,不处理 
  6.         // 第1步、使用File类找到一个文件 
  7.         File f= new File("d:" + File.separator + "test.txt") ;  // 声明File对象 
  8.         // 第2步、通过子类实例化父类对象 
  9.         InputStream input = null ;  // 准备好一个输入的对象 
  10.         input = new FileInputStream(f)  ;   // 通过对象多态性,进行实例化 
  11.         // 第3步、进行读操作 
  12.         byte b[] = new byte[1024] ;     // 数组大小由文件决定 
  13.         int len = 0 ;  
  14.         int temp = 0 ;          // 接收每一个读取进来的数据 
  15.         while((temp=input.read())!=-1){ 
  16.             // 表示还有内容,文件没有读完 
  17.             b[len] = (byte)temp ; 
  18.             len++ ; 
  19.         } 
  20.         // 第4步、关闭输出流 
  21.         input.close() ;                     // 关闭输出流\ 
  22.         System.out.println("内容为:" + new String(b,0,len)) ;  // 把byte数组变为字符串输出 
  23.     } 
  24. }; 

二、输出流

1、

 
  1. import java.io.File ; 
  2. import java.io.OutputStream ; 
  3. import java.io.FileOutputStream ; 
  4. public class OutputStreamDemo01{ 
  5.     public static void main(String args[]) throws Exception{    // 异常抛出,不处理 
  6.         // 第1步、使用File类找到一个文件 
  7.         File f= new File("d:" + File.separator + "test.txt") ;  // 声明File对象 
  8.         // 第2步、通过子类实例化父类对象 
  9.         OutputStream out = null ;   // 准备好一个输出的对象 
  10.         out = new FileOutputStream(f)  ;    // 通过对象多态性,进行实例化 
  11.         // 第3步、进行写操作 
  12.         String str = "Hello World!!!" ;     // 准备一个字符串 
  13.         byte b[] = str.getBytes() ;         // 只能输出byte数组,所以将字符串变为byte数组 
  14.         out.write(b) ;                      // 将内容输出,保存文件 
  15.         // 第4步、关闭输出流 
  16.         out.close() ;                       // 关闭输出流 
  17.     } 
  18. }; 

2、

 
  1. import java.io.File ; 
  2. import java.io.OutputStream ; 
  3. import java.io.FileOutputStream ; 
  4. public class OutputStreamDemo02{ 
  5.     public static void main(String args[]) throws Exception{    // 异常抛出,不处理 
  6.         // 第1步、使用File类找到一个文件 
  7.         File f= new File("d:" + File.separator + "test.txt") ;  // 声明File对象 
  8.         // 第2步、通过子类实例化父类对象 
  9.         OutputStream out = null ;   // 准备好一个输出的对象 
  10.         out = new FileOutputStream(f)  ;    // 通过对象多态性,进行实例化 
  11.         // 第3步、进行写操作 
  12.         String str = "Hello World!!!" ;     // 准备一个字符串 
  13.         byte b[] = str.getBytes() ;         // 只能输出byte数组,所以将字符串变为byte数组 
  14.         for(int i=0;i<b.length;i++){        // 采用循环方式写入 
  15.             out.write(b[i]) ;   // 每次只写入一个内容 
  16.         } 
  17.         // 第4步、关闭输出流 
  18.         out.close() ;                       // 关闭输出流 
  19.     } 
  20. }; 

3、

 
  1. import java.io.File ; 
  2. import java.io.OutputStream ; 
  3. import java.io.FileOutputStream ; 
  4. public class OutputStreamDemo03{ 
  5.     public static void main(String args[]) throws Exception{    // 异常抛出,不处理 
  6.         // 第1步、使用File类找到一个文件 
  7.         File f= new File("d:" + File.separator + "test.txt") ;  // 声明File对象 
  8.         // 第2步、通过子类实例化父类对象 
  9.         OutputStream out = null ;   // 准备好一个输出的对象 
  10.         out = new FileOutputStream(f,true)  ;   // 此处表示在文件末尾追加内容 
  11.         // 第3步、进行写操作 
  12.         String str = "Hello World!!!" ;     // 准备一个字符串 
  13.         byte b[] = str.getBytes() ;         // 只能输出byte数组,所以将字符串变为byte数组 
  14.         for(int i=0;i<b.length;i++){        // 采用循环方式写入 
  15.             out.write(b[i]) ;   // 每次只写入一个内容 
  16.         } 
  17.         // 第4步、关闭输出流 
  18.         out.close() ;                       // 关闭输出流 
  19.     } 
  20. }; 

4、

 
  1. import java.io.File ; 
  2. import java.io.OutputStream ; 
  3. import java.io.FileOutputStream ; 
  4. public class OutputStreamDemo04{ 
  5.     public static void main(String args[]) throws Exception{    // 异常抛出,不处理 
  6.         // 第1步、使用File类找到一个文件 
  7.         File f= new File("d:" + File.separator + "test.txt") ;  // 声明File对象 
  8.         // 第2步、通过子类实例化父类对象 
  9.         OutputStream out = null ;   // 准备好一个输出的对象 
  10.         out = new FileOutputStream(f,true)  ;   // 此处表示在文件末尾追加内容 
  11.         // 第3步、进行写操作 
  12.         String str = "\r\nHello World!!!" ;     // 准备一个字符串 
  13.         byte b[] = str.getBytes() ;         // 只能输出byte数组,所以将字符串变为byte数组 
  14.         for(int i=0;i<b.length;i++){        // 采用循环方式写入 
  15.             out.write(b[i]) ;   // 每次只写入一个内容 
  16.         } 
  17.         // 第4步、关闭输出流 
  18.         out.close() ;                       // 关闭输出流 
  19.     } 
  20. }; 

5、

 
  1. import java.io.File ; 
  2. import java.io.OutputStream ; 
  3. import java.io.FileOutputStream ; 
  4. public class OutputStreamDemo05{ 
  5.     public static void main(String args[]) throws Exception{    // 异常抛出,不处理 
  6.         // 第1步、使用File类找到一个文件 
  7.         File f= new File("d:" + File.separator + "test.txt") ;  // 声明File对象 
  8.         // 第2步、通过子类实例化父类对象 
  9.         OutputStream out = null ;   // 准备好一个输出的对象 
  10.         out = new FileOutputStream(f)  ;    // 实例化 
  11.         // 第3步、进行写操作 
  12.         String str = "Hello World!!!" ;     // 准备一个字符串 
  13.         byte b[] = str.getBytes() ;         // 只能输出byte数组,所以将字符串变为byte数组 
  14.         out.write(b) ;      // 写入数据 
  15.         // 第4步、关闭输出流 
  16.         // out.close() ;                        // 关闭输出流 
  17.     } 
  18. }; 

三、输入输出流转换

 (1)对象的输入输出流ObjectOutputStream、ObjectInputStream, 可以序列化对象或反序列化对象

【注意】(1)必须实现Serializable接口 (2)序列化的类不能是内部类

 
  1. Student student = new Student(); 
  2.     student.setName("zhangsan"); 
  3.     student.setAge(10); 
  4.      
  5.     ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("d:" + File.separator + "test.txt"))); 
  6.     oos.writeObject(student); 
  7.     oos.close(); 
  8.      
  9.     ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("d:" + File.separator + "test.txt"))); 
  10.     Student stu = (Student) ois.readObject(); 
  11.     System.out.println(stu.getName() + "\t" + stu.getAge()); 
  12.     ois.close(); 

 (2)序列化和反序列化一组对象

 
  1. public class SerDemo{ 
  2.     public static void main(String args[]) throws Exception{ 
  3.         Person per[] = {new Person("张三",30),new Person("李四",31), 
  4.             new Person("王五",32)} ; 
  5.         ser(per) ; 
  6.         Object o[] = (Object[])dser() ; 
  7.         for(int i=0;i<o.length;i++){ 
  8.             Person p = (Person)o[i] ; 
  9.             System.out.println(p) ; 
  10.         } 
  11.     } 
  12.     public static void ser(Object obj[]) throws Exception { 
  13.         File f = new File("D:" + File.separator + "test.txt") ; // 定义保存路径 
  14.         ObjectOutputStream oos = null ; // 声明对象输出流 
  15.         OutputStream out = new FileOutputStream(f) ;    // 文件输出流 
  16.         oos = new ObjectOutputStream(out) ; 
  17.         oos.writeObject(obj) ;  // 保存对象 
  18.         oos.close() ;   // 关闭 
  19.     } 
  20.     public static Object[] dser() throws Exception { 
  21.         File f = new File("D:" + File.separator + "test.txt") ; // 定义保存路径 
  22.         ObjectInputStream ois = null ;  // 声明对象输入流 
  23.         InputStream input = new FileInputStream(f) ;    // 文件输入流 
  24.         ois = new ObjectInputStream(input) ;    // 实例化对象输入流 
  25.         Object obj[] = (Object[])ois.readObject() ; // 读取对象 
  26.         ois.close() ;   // 关闭 
  27.         return obj ; 
  28.     } 
  29. }; 

 

 

四、文件的拷贝

(1)本地文件的拷贝

JDK中没有提供文件拷贝的相关方法,apache的commons下面有关于File的扩展FileUtils可以做到。具体参考如下链接:http://commons.apache.org/io/api-release/index.html

 (2)远程文件拷贝(大致思路:InputStream -> HttpURLConnection -> OutputStream

 
  1. public class HttpDistributeFileReceiverImpl implements DistributeFileReceiver { 
  2.      
  3.     private static final Log logger = LogFactory.getLog(HttpDistributeFileReceiverImpl.class); 
  4.      
  5.     public void receive(String targetURL, String targetFileType, String targetDirPath, String targetFileNamePrefix) throws FileReceiveException { 
  6.          
  7.         if (!isParameterValid(targetDirPath) || !isParameterValid(targetFileNamePrefix) || !isParameterValid(targetURL) || !isParameterValid(targetFileType)) { 
  8.             if (logger.isInfoEnabled()) { 
  9.                 logger.info("parameter is invalid!"); 
  10.             } 
  11.             return
  12.         } 
  13.          
  14.         if (logger.isDebugEnabled()) { 
  15.             logger.debug("download from " + targetURL); 
  16.         } 
  17.          
  18.         try { 
  19.             prepare(targetDirPath); 
  20.         } catch (PrepareDistributeEnvException e) { 
  21.             throw new FileReceiveException(e); 
  22.         } 
  23.          
  24.         String newTargetFileName = targetFileNamePrefix + ".tar"
  25.         String targetFileFullPath = targetDirPath + File.separator + newTargetFileName; 
  26.         File targetFile = null
  27.         FileOutputStream outputStream = null;    
  28.          
  29.         try { 
  30.             targetFile = new File(targetFileFullPath); 
  31.             if (logger.isDebugEnabled()) { 
  32.                 logger.debug("create file '" + targetFileFullPath + "' ......"); 
  33.             } 
  34.             outputStream = new FileOutputStream(targetFile); 
  35.         } catch (FileNotFoundException e) { 
  36.             throw new FileReceiveException(e); 
  37.         } 
  38.          
  39.          
  40.         URL url = null
  41.         try { 
  42.             url = new URL(targetURL); 
  43.         } catch (MalformedURLException e) { 
  44.             throw new FileReceiveException("download file '" + targetURL + "' failed!", e);          
  45.         } 
  46.          
  47.         HttpURLConnection connection = null
  48.         InputStream inputStream = null
  49.          
  50.         try { 
  51.              
  52.             connection = (HttpURLConnection)url.openConnection(); 
  53.             inputStream = connection.getInputStream();       
  54.          
  55.             byte[] buffer = new byte[1024];          
  56.             int len = 0;             
  57.             while ((len = inputStream.read(buffer, 01024)) != -1 ) {               
  58.                 outputStream.write(buffer, 0, len);                                  
  59.             }; 
  60.              
  61.             if (logger.isDebugEnabled()) { 
  62.                 logger.debug("create file '" + targetFileFullPath + "' success!"); 
  63.             } 
  64.              
  65.         } catch (IOException e) {            
  66.              
  67.             throw new FileReceiveException("download file '" + targetURL + "' failed!", e); 
  68.              
  69.         } finally { 
  70.             if (outputStream != null) { 
  71.                 try { 
  72.                     outputStream.close(); 
  73.                 } catch (IOException e) { 
  74.                     if (logger.isWarnEnabled()) { 
  75.                         logger.warn("close outputStream failed!\n"  
  76.                                 + ExceptionUtil.getThrowableStackTrace(e)); 
  77.                     } 
  78.                 } 
  79.             } 
  80.             if (inputStream != null) { 
  81.                 try { 
  82.                     inputStream.close(); 
  83.                 } catch (IOException e) {                    
  84.                     if (logger.isWarnEnabled()) { 
  85.                         logger.warn("close inputStream failed!\n"  
  86.                                 + ExceptionUtil.getThrowableStackTrace(e)); 
  87.                     } 
  88.                 } 
  89.             } 
  90.             if (connection != null) { 
  91.                 connection.disconnect(); 
  92.             } 
  93.         } 
  94.          
  95.         try { 
  96.             if (MessageConstant.TARGET_PACKAGE_TYPE_FOR_ZIP.equals(targetFileType)) { 
  97.                 FileUtil.unzip(targetFileFullPath, targetDirPath); 
  98.             } else if (MessageConstant.TARGET_PACKAGE_TYPE_FOR_TAR.equals(targetFileType)) { 
  99.                 FileUtil.untar(targetFileFullPath, targetDirPath); 
  100.             } else {             
  101.                 throw new FileReceiveException("unpackage failed, unknown package file type'" + targetFileType + "'"); 
  102.             } 
  103.              
  104.         } catch(UnzipFileException e) { 
  105.             throw new FileReceiveException(e); 
  106.         } 
  107.          
  108.     } 
  109.      
  110.     /** 
  111.      * 为预发布准备环境目录 
  112.      * @param targetWorkingDir 
  113.      * @throws PrepareDistributeEnvException 
  114.      */ 
  115.     private void prepare(String targetWorkingDir) throws PrepareDistributeEnvException{ 
  116.          
  117.         if (logger.isDebugEnabled()) { 
  118.             logger.debug("try to locate target dir '" + targetWorkingDir + "'"); 
  119.         } 
  120.          
  121.         File targetDir = new File(targetWorkingDir);  
  122.          
  123.         //if target dir exists 
  124.         if (targetDir.exists()) { 
  125.             //如果是目录 
  126.             if (targetDir.isDirectory()) { 
  127.                 if (logger.isDebugEnabled()) { 
  128.                     logger.debug("target dir '" + targetWorkingDir + "' already exist!"); 
  129.                 } 
  130.                 //清除目标目录 
  131.                 try { 
  132.                     FileUtil.deleteDirs(targetDir); 
  133.                 } catch (DeleteDirException e) {                     
  134.                     throw new PrepareDistributeEnvException("delete target dir failed!", e);                     
  135.                 }                
  136.              
  137.             } else { 
  138.                 //如果是文件 
  139.                 if (logger.isDebugEnabled()) { 
  140.                     logger.debug("target dir '" + targetWorkingDir + "' alread exists, but is file type"); 
  141.                 } 
  142.                 //删除同名文件 
  143.                 targetDir.delete(); 
  144.             } 
  145.              
  146.             //创建目标文件夹 
  147.             if (!targetDir.mkdirs()) { 
  148.                 if (logger.isDebugEnabled()) { 
  149.                     logger.debug("create dir '" + targetWorkingDir + "' failed!"); 
  150.                 } 
  151.                 throw new PrepareDistributeEnvException("create target dir failed!");    
  152.             } 
  153.              
  154.             if (logger.isDebugEnabled()) { 
  155.                 logger.debug("create target dir '" + targetWorkingDir + "' success!"); 
  156.             } 
  157.             return;          
  158.              
  159.         }    
  160.          
  161.         //if target dir doesn't exist 
  162.         if (targetDir.mkdirs()) { 
  163.             if (logger.isDebugEnabled()) { 
  164.                 logger.debug("create target dir '" + targetWorkingDir + "' success!"); 
  165.             } 
  166.             return
  167.         } else { 
  168.             if (logger.isDebugEnabled()) { 
  169.                 logger.debug("create target target dir '" + targetWorkingDir + "' failed!"); 
  170.             } 
  171.             throw new PrepareDistributeEnvException("create target dir failed!"); 
  172.         } 
  173.          
  174.          
  175.     } 
  176.      
  177.     /** 
  178.      * 判断模型release包下载路径和参数是否有效 
  179.      * @return 
  180.      */ 
  181.     private boolean isParameterValid(String parameter) { 
  182.          
  183.         if (StringUtil.isEmpty(parameter) || StringUtil.isBlank(parameter) ) { 
  184.              
  185.             return false
  186.         } 
  187.          
  188.         return true
  189.     }    
  190.      

 

 


本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/427130,如需转载请自行联系原作者

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
29天前
|
缓存 Java
JAVA带缓存的输入输出流
JAVA带缓存的输入输出流
18 0
|
11天前
|
Java 程序员 调度
Java中的多线程编程:基础知识与实践
【4月更文挑战第5天】 在现代软件开发中,多线程编程是一个不可或缺的技术要素。它允许程序员编写能够并行处理多个任务的程序,从而充分利用多核处理器的计算能力,提高应用程序的性能。Java作为一种广泛使用的编程语言,提供了丰富的多线程编程支持。本文将介绍Java多线程编程的基础知识,并通过实例演示如何创建和管理线程,以及如何解决多线程环境中的常见问题。
|
16天前
|
关系型数据库 Java 开发工具
Java入门高频考查基础知识9(15问万字参考答案)
本文探讨了Spring Cloud的工作原理,包括注册中心的心跳机制、服务发现机制,以及Eureka默认的负载均衡策略。同时,概述了Spring Boot中常用的注解及其实现方式,并深入讨论了Spring事务的注解、回滚条件、传播性和隔离级别。文章还介绍了MySQL的存储引擎及其区别,特别关注了InnoDB如何实现MySQL的事务处理。此外,本文还详细探讨了MySQL索引,包括B+树的原理和设计索引的方法。最后,比较了Git和SVN的区别,并介绍了Git命令的底层原理及流程。
28 0
Java入门高频考查基础知识9(15问万字参考答案)
|
16天前
|
存储 缓存 算法
Java入门高频考查基础知识4(字节跳动面试题18题2.5万字参考答案)
最重要的是保持自信和冷静。提前准备,并对自己的知识和经验有自信,这样您就能在面试中展现出最佳的表现。祝您面试顺利!Java 是一种广泛使用的面向对象编程语言,在软件开发领域有着重要的地位。Java 提供了丰富的库和强大的特性,适用于多种应用场景,包括企业应用、移动应用、嵌入式系统等。下是几个面试技巧:复习核心概念、熟悉常见问题、编码实践、项目经验准备、注意优缺点、积极参与互动、准备好问题问对方和知其所以然等,多准备最好轻松能举一反三。
46 0
Java入门高频考查基础知识4(字节跳动面试题18题2.5万字参考答案)
|
16天前
|
存储 算法 JavaScript
Java入门高频考查算法逻辑基础知识3-编程篇(超详细18题1.8万字参考编程实现)
解决这类问题时,建议采取下面的步骤: 理解数学原理:确保你懂得基本的数学公式和法则,这对于制定解决方案至关重要。 优化算法:了解时间复杂度和空间复杂度,并寻找优化的机会。特别注意避免不必要的重复计算。 代码实践:多编写实践代码,并确保你的代码是高效、清晰且稳健的。 错误检查和测试:要为你的代码编写测试案例,测试标准的、边缘情况以及异常输入。 进行复杂问题简化:面对复杂的问题时,先尝试简化问题,然后逐步分析和解决。 沟通和解释:在编写代码的时候清晰地沟通你的思路,不仅要写出正确的代码,还要能向面试官解释你的
32 0
|
16天前
|
存储 Java 编译器
Java入门高频考查基础知识2(超详细28题2.5万字答案)
多态是面向对象编程中的一个重要概念,它允许不同类的对象对同一消息作出不同的响应。在具体实现上,多态允许一个父类的引用指向其子类的对象,并根据实际指向的对象的类型来调用相应的方法。在 Java 中,多态可以通过以下几种方式实现:在同一个类中,方法名相同,但形参列表不同,实现了多态。子类可以重写(覆盖)其父类的方法,实现多态。在父类引用中调用该方法时,根据实际指向的子类对象的类型来调用相应的方法实现。
38 0
|
22天前
|
Java 数据库连接 API
Java 学习路线:基础知识、数据类型、条件语句、函数、循环、异常处理、数据结构、面向对象编程、包、文件和 API
Java 是一种广泛使用的、面向对象的编程语言,始于1995年,以其跨平台性、安全性和可靠性著称,应用于从移动设备到数据中心的各种场景。基础概念包括变量(如局部、实例和静态变量)、数据类型(原始和非原始)、条件语句(if、else、switch等)、函数、循环、异常处理、数据结构(如数组、链表)和面向对象编程(类、接口、继承等)。深入学习还包括包、内存管理、集合框架、序列化、网络套接字、泛型、流、JVM、垃圾回收和线程。构建工具如Gradle、Maven和Ant简化了开发流程,Web框架如Spring和Spring Boot支持Web应用开发。ORM工具如JPA、Hibernate处理对象与数
88 3
|
29天前
|
SQL Java 数据库连接
JAVA数据库的基础知识
JAVA数据库的基础知识
14 1
|
29天前
|
Java
JAVA输入输出流
JAVA输入输出流
12 1
|
30天前
|
Java 调度
Java中的多线程编程:基础知识与实践
【2月更文挑战第26天】在现代软件开发中,多线程编程是一个重要的概念。Java作为一种广泛使用的编程语言,提供了丰富的多线程编程支持。本文将介绍Java中多线程编程的基础知识,包括线程的概念、创建和控制,以及线程同步和通信的方法。同时,通过实例分析,帮助读者更好地理解和掌握Java多线程编程的技巧。