Java 杂记
文件拷贝两种方式
//不断内存拷贝,大文件增加cpu负担 public static void copyFile(String srcFile, String destFile) throws Exception { byte[] temp = new byte[1024]; FileInputStream in = new FileInputStream(srcFile); FileOutputStream out = new FileOutputStream(destFile); int length; while ((length = in.read(temp)) != -1) { out.write(temp, 0, length); } in.close(); out.close(); } //减少内存拷贝 public static void copyFileWithFileChannel(String srcFileName, String destFileName) throws Exception { RandomAccessFile srcFile = new RandomAccessFile(srcFileName, "r"); FileChannel srcFileChannel = srcFile.getChannel(); RandomAccessFile destFile = new RandomAccessFile(destFileName, "rw"); FileChannel destFileChannel = destFile.getChannel(); long position = 0; long count = srcFileChannel.size(); srcFileChannel.transferTo(position, count, destFileChannel); }
### 查看java进程的GC堆内存信息
```
idea运行的程序使用的是内置jvm,要cmd运行程序或服务器程序:
1. 找到进程ID: ps -ef|grep java
2.jmap -heap 32643
```
```
JVM version is 25.73-b02
using thread-local object allocation.
Parallel GC with 4 thread(s)
Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 2099249152 (2002.0MB)
NewSize = 44040192 (42.0MB)
MaxNewSize = 699400192 (667.0MB)
OldSize = 88080384 (84.0MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 21807104 (20.796875MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
PS Young Generation
Eden Space:
capacity = 24641536 (23.5MB)
used = 4691056 (4.4737396240234375MB)
free = 19950480 (19.026260375976562MB)
19.037189889461438% used
From Space:
capacity = 524288 (0.5MB)
used = 32768 (0.03125MB)
free = 491520 (0.46875MB)
6.25% used
To Space:
capacity = 524288 (0.5MB)
used = 0 (0.0MB)
free = 524288 (0.5MB)
0.0% used
PS Old Generation
capacity = 47185920 (45.0MB)
used = 16827568 (16.048019409179688MB)
free = 30358352 (28.951980590820312MB)
35.662265353732636% used
```
```
将jar添加到本地仓库的做法:
以下面pom.xml依赖的jar包为例:
实际项目中pom.xml依赖写法:
[html] view plain copy
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
Maven 安装 JAR 包的命令是:
[html] view plain copy
mvn install:install-file
-Dfile=jar包的位置
-DgroupId=上面的groupId
-DartifactId=上面的artifactId
-Dversion=上面的version
-Dpackaging=jar
例如我的这个spring-context-support-3.1.0.RELEASE.jar 文件放在了"D:\mvn\"中
则命令为:
mvn install:install-file
-Dfile=D:\mvn\spring-context-support-3.1.0.RELEASE.jar
-DgroupId=org.springframework
-DartifactId=spring-context-support
-Dversion=3.1.0.RELEASE
-Dpackaging=jar
```
```
maven 打包 跳过测试
mvn clean package appassembler:generate-daemons -Dmaven.test.skip=true
上传服务器
scp -r target/generate-resource/.... flk@192.168.1.1:/root/kms/
```