Last active
March 15, 2022 08:11
-
-
Save renfei/892fd0a3b73e2f618d6174219a838996 to your computer and use it in GitHub Desktop.
Java学习记录
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 冒泡排序 | |
| */ | |
| static class BubbleSort { | |
| public static void main(String[] args) { | |
| int[] arr = new int[]{1, 3, 5, 0, 7, -8, 4, 1, 8, 9, 12, 2}; | |
| for (int i = 0; i < arr.length - 1; i++) { | |
| for (int j = 0; j < arr.length - 1 - i; j++) { | |
| if (arr[j] > arr[j + 1]) { | |
| // 如果左侧的比右侧的大,那么交换 | |
| arr[j] ^= arr[j + 1]; | |
| arr[j + 1] ^= arr[j]; | |
| arr[j] ^= arr[j + 1]; | |
| } | |
| } | |
| } | |
| System.out.println(Arrays.toString(arr)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 写一个方法,输入一个文件名和一个字符串,统计这个字符串在这个文件中出现的次数 | |
| * | |
| * @param fileName | |
| * @param word | |
| * @return | |
| * @throws IOException | |
| */ | |
| long countWordInFile(String fileName, String word) throws IOException { | |
| long counter = 0; | |
| try (FileReader reader = new FileReader(fileName)) { | |
| try (BufferedReader bufferedReader = new BufferedReader(reader)) { | |
| String line; | |
| while ((line = bufferedReader.readLine()) != null) { | |
| if (line.length() > word.length() && line.contains(word)) { | |
| counter++; | |
| } | |
| } | |
| } | |
| } | |
| return counter; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 编程实现文件拷贝 | |
| * | |
| * @param source | |
| * @param target | |
| * @throws IOException | |
| */ | |
| void fileCopy(String source, String target) throws IOException { | |
| try (InputStream inputStream = new FileInputStream(source)) { | |
| try (OutputStream outputStream = new FileOutputStream(target)) { | |
| byte[] buf = new byte[1024]; | |
| int bufToRead; | |
| while ((bufToRead = inputStream.read(buf)) != -1) { | |
| outputStream.write(buf, 0, bufToRead); | |
| } | |
| } | |
| } | |
| } | |
| /** | |
| * 编程实现文件拷贝 | |
| * | |
| * @param source | |
| * @param target | |
| * @throws IOException | |
| */ | |
| void fileCopyNIO(String source, String target) throws IOException { | |
| try (FileInputStream in = new FileInputStream(source)) { | |
| try (FileOutputStream out = new FileOutputStream(target)) { | |
| FileChannel inFileChannel = in.getChannel(); | |
| FileChannel outFileChannel = out.getChannel(); | |
| ByteBuffer byteBuffer = ByteBuffer.allocate(1024); | |
| while (inFileChannel.read(byteBuffer) != -1) { | |
| byteBuffer.flip(); | |
| outFileChannel.write(byteBuffer); | |
| byteBuffer.clear(); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * JDBC 的连接案例 | |
| * | |
| * @throws ClassNotFoundException | |
| */ | |
| void jdbcDemo() throws ClassNotFoundException { | |
| // 加载驱动 | |
| Class.forName("com.mysql.cj.jdbc.Driver"); | |
| // 建立连接 | |
| try (Connection connection = DriverManager.getConnection("")) { | |
| // 构造语句对象 | |
| PreparedStatement ps = connection.prepareStatement("select * from demo where id > ? and id < ?"); | |
| ps.setInt(1, 23); | |
| ps.setInt(2, 10); | |
| // 事务处理 | |
| // connection.setAutoCommit(false); | |
| ResultSet resultSet = ps.executeQuery(); | |
| // connection.commit(); | |
| // connection.rollback(); | |
| while (resultSet.next()) { | |
| System.out.println(resultSet.getString("name")); | |
| } | |
| } catch (SQLException e) { | |
| e.printStackTrace(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 双重校验锁实现单例模式 | |
| */ | |
| static class Singleton { | |
| private static volatile Singleton singleton = null; | |
| private Singleton() { | |
| } | |
| public static Singleton getInstance() { | |
| // 第一次校验,这里不加锁,毕竟只有第一次是 null,其他情况无需上锁 | |
| if (singleton == null) { | |
| // 先上锁,确保只放进来一个进程 | |
| synchronized (Singleton.class) { | |
| // 第二次校验,这里只有一个进程能进来 | |
| if (singleton == null) { | |
| singleton = new Singleton(); | |
| } | |
| } | |
| } | |
| return singleton; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment