Skip to content

Commit

Permalink
xxx
Browse files Browse the repository at this point in the history
  • Loading branch information
李福春 committed Mar 26, 2020
1 parent 23ce845 commit aac70dc
Show file tree
Hide file tree
Showing 11 changed files with 680 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mianshi/mianshi.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />
75 changes: 75 additions & 0 deletions mianshi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example.mianshi</groupId>
<artifactId>mianshi</artifactId>
<version>1.0-SNAPSHOT</version>

<name>mianshi</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
45 changes: 45 additions & 0 deletions mianshi/src/main/java/org/example/mianshi/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.example.mianshi;

import java.util.LinkedHashMap;
import java.util.Map;

/**
* 不常使用的资源被释放掉
*
*/
public class App
{
public static void main( String[] args )
{

LinkedHashMap<String,String> linkedHashMap = new LinkedHashMap<String,String>(){

@Override
protected boolean removeEldestEntry(Map.Entry<String, String> eldest) {
return size()>3;
}
};

linkedHashMap.put("a","aaa");
linkedHashMap.put("b","bbb");
linkedHashMap.put("c","ccc");


linkedHashMap.forEach((k,v)->System.out.println(k+" = " + v));


System.out.println(linkedHashMap.get("a"));
System.out.println(linkedHashMap.get("b"));
System.out.println(linkedHashMap.get("c"));

linkedHashMap.forEach((k,v)->System.out.println(k+" = " + v));

linkedHashMap.put("d","ddd");

System.out.println("=========");

linkedHashMap.forEach((k,v)->System.out.println(k+" = " + v));


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.example.mianshi.filecopy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

/**
* 说明:Files的文件copy
* @author carter
* 创建时间: 2020年03月26日 9:32 上午
**/

public class FilesFileCopyApp {

public static void main(String[] args) {

final File d = new File("/data/appenvs/fenv.properties");
final File s = new File("/data/appenvs/env.properties");

System.out.println("source file content :" + s.exists());
System.out.println("target file content :" + d.exists());

System.out.println("source content:");
try {
Files.lines(s.toPath()).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}

System.out.println("do file copy !");

copy(s, d);

System.out.println("target file content :" + d.exists());
System.out.println("target content:");
try {
Files.lines(d.toPath()).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}

}

private static void copy(File s, File d) {

try {
Files.copy(s.toPath(),d.toPath(), StandardCopyOption.COPY_ATTRIBUTES);
} catch (IOException e) {
e.printStackTrace();
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.example.mianshi.filecopy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;

/**
* 说明:传统的文件copy
* @author carter
* 创建时间: 2020年03月26日 9:32 上午
**/

public class JioFileCopyApp {

public static void main(String[] args) {

final File d = new File("/data/appenvs/denv.properties");
final File s = new File("/data/appenvs/env.properties");

System.out.println("source file content :" + s.exists());
System.out.println("target file content :" + d.exists());

System.out.println("source content:");
try {
Files.lines(s.toPath()).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}

System.out.println("do file copy !");

copy(s, d);

System.out.println("target file content :" + d.exists());
System.out.println("target content:");
try {
Files.lines(d.toPath()).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}

}

private static void copy(File s, File d) {

try (
final FileInputStream fileInputStream = new FileInputStream(s);

final FileOutputStream fileOutputStream = new FileOutputStream(d)
) {

byte[] buffer = new byte[1024];
int length;
while ((length = fileInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, length);
}

} catch (IOException e) {
e.printStackTrace();
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.example.mianshi.filecopy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;

/**
* 说明:传统的文件copy
* @author carter
* 创建时间: 2020年03月26日 9:32 上午
**/

public class JnioFileCopyApp {

public static void main(String[] args) {

final File d = new File("/data/appenvs/ndenv.properties");
final File s = new File("/data/appenvs/env.properties");

System.out.println(s.getAbsolutePath() + "source file content :" + s.exists());
System.out.println(d.getAbsolutePath() +"target file content :" + d.exists());

System.out.println("source content:");
try {
Files.lines(s.toPath()).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}

System.out.println("do file copy !");

copy(s, d);

System.out.println(d.getAbsolutePath() +"target file content :" + d.exists());
System.out.println("target content:");
try {
Files.lines(d.toPath()).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}

}

private static void copy(File s, File d) {

try (
final FileChannel sourceFileChannel = new FileInputStream(s).getChannel();

final FileChannel targetFileChannel = new FileOutputStream(d).getChannel()
) {

for (long count= sourceFileChannel.size();count>0;){

final long transferTo = sourceFileChannel.transferTo(sourceFileChannel.position(), count, targetFileChannel);

count-=transferTo;

}

} catch (IOException e) {
e.printStackTrace();
}

}

}
Loading

0 comments on commit aac70dc

Please sign in to comment.