1.拷贝一张图片,到另外一个目录下
package Java0111.Work; import java.io.*; public class Code1 { public static void main(String[] args) throws Exception { //1.拷贝一张图片,到另外一个目录下 FileInputStream fis = new FileInputStream("E;\\test\\1.png"); BufferedInputStream bis = new BufferedInputStream(fis); File file = new File( "E:\\test2"); file.mkdir(); System.out.println(file.getAbsolutePath()); FileOutputStream fos = new FileOutputStream(file.getAbsolutePath()+"\\1.png" ); BufferedOutputStream bos = new BufferedOutputStream(fos); int readNum = -1; while ((readNum = bis.read()) != -1){ bos.write(readNum) ; } bis.close(); bos.close(); fis.close(); fos.close(); } }
2.在网上找一张图片,复制图片的网络地址,然后写一个Java程序下载该图片到本地。
package Java0111.Work; import java.io.*; import java.net.URL; import java.net.URLConnection; public class Code2 {
public static void main(String[] args) throws IOException { URL url = new URL( "https://t7. baidu. com/it/u=2168645659。3174029352&fm=193&f=GIF"); URLConnection connection = url. openConnection(); InputStream is = connection. getInputStream() ; BufferedInputStream bis = new BufferedInputStream(is); FileOutputStream fos = new FileOutputStream("E: \\test\\2.gif"); BufferedOutputStream bos = new BufferedOutputStream(fos); int readNum = -1; while ((readNum = bis.read()) != -1){ bos . write(readNum); bis. close(); bos. close(); is.close(); fos.close(); } } }
3. 有2个数组,第一个数组内容为:[黑龙江省,浙江省,江西省,广东省,福建省],第二个数组为:[哈尔滨,杭州,南昌,广州,福州],将第一个数组元素作为key,第二个数组元素作为value存储到Map集合中。如{黑龙江省=哈尔滨, 浙江省=杭州, …}。
package Java0111.Work; import java.util.HashMap; import java.util.Map; public class Code3 { public static void main(String[] args) { String[] provinces = {"黑龙江省","浙江省","江西省","广东省","福建省"}; String[] cities = {"哈尔滨","杭州","南昌","广州","福州"}; Map<String , String > map = new HashMap<>(); for (int i = 0; i < provinces.length; i++) { map.put(provinces[i] , cities[i]); } System.out.println(map); } }
4. 创建一个List集合的对象,添加几条数据,将1号位和2号位交换;获得最大值,最小值打印出来,最后再遍历该集合并把元素打印出来
package Java0111.Work; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Code4 { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); for(int i=0;i<10;i++){ list.add(i); } System.out.println(list); Collections.swap(list,0 ,1); System.out.println(list); System.out.println(Collections . max(list)); System.out.println(Collections . min(list)); for (Integer i : list){ System.out.println(i); } } }