1. 程式人生 > >學習筆記--Java

學習筆記--Java

ade layout erl har 從0到1 rec cas 時間 implement

//輸入輸出Scanner in = new Scanner(System.in);int a = in.nextInt();int b = in.next();System.out.println(in.nextLine());
//輸入流 InputStreamSystem.in.read(buffer); //讀取輸入流存到buffer(byte[]類型)中System.in.read(); //讀一個字節,若結尾返回-1System.in.read(buffer, int off, int len); // 讀len個字節,從buffer[off]開始存System.in.skip(n);
//輸入流跳過n個字節System.in.available();System.in.mark(); //標記System.in.reset(); //返回標記處System.in.markSupported; //是否支持標記System.in.close(); //關閉輸入流
//類java.io.StreamTokenizer可以獲取輸入流並將其分析為Token(標記)。StreamTokenizer的nextToken方法將讀取下一個標記,下一個標記的類型在ttype字段中返回。關於令牌的附加信息可能是在nval字段或標記生成器的sval 字段,結束標誌為
TT_EOF。
StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
st.nextToken();int m=(int)st.nval;
//輸出流 OutputStreamSystem.out.write(int b);System.out.write(byte[] b);System.out.write(byte[] b, int off, int len);System.out.flush();System.out.close();
//文件流//二進制數據讀寫(字節流)
FileOutputStream out = new FileOutputStream("a.txt"); //根據字節讀寫數據out.write(buffer);out.close(); FileInputStream in = new FileInputStream("a.txt");in.read();in.close();DataOutputStream out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream("a.txt"))); //讀寫基本類型數據out.writeInt(int a);DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream("a.txt")));
in.readInt();//文本數據讀寫(字符流)PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( new FileOutputStream("a.txt")))); //輸出文本數據out.println("abcdefg");out.format("格式",...);out.printf("格式", ...);out.print(基本類型);
BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream("a.txt"))); //讀文本數據
String line = in.readLine();in.getLineNumber();FileReader in = new FileReader(String fileName); //在給定從中讀取數據的文件名的情況下新建FileReaderFileReader in = new FileReader(File file); //在給定從中讀取數據的File的情況下新建FileReader//轉換編碼InputStreamReader(InputStream in, String charsetName); //創建使用指定字符集的InputStreamReader //網絡端口連接讀寫數據Socket socket = new Socket(InetAddress.getByName("localhost"), 12345); //建立與本地12345端口的連接PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream()))); //建立對socket的輸出流//讀寫對象class Student implements Serializable {...} //類實現Serializable串行化接口ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("a.dat")); //對象輸出流out.writeObject(Student stu1);ObjectInputStream in = new ObjectInputStream( new FileInputStream("a.dat"); //對象輸入流out.readObject(Student stu2); //字符串操作String s = new String();s.equals("abc");s1.compareTo(s2);s.substring(n);s.substring(n1, n2);s.charAt(index);s.indexOf(c);s.indexOf(c, n);s.indexOf(t); //也可以尋找字符串位置s.lastIndexOf(c); //從右邊開始找s.startsWith(t); //是否由字符串t開始s.endsWith(t); //是否由字符串t結尾s.trim(); //把字符串兩端的空格刪掉s.replace(c1, c2); //把s中所有的c1都換成c2s.toLowerCase(); //把s中所有的大寫字母換成小寫字母s.toUpperCase(); //把s中所有的小寫字母換成大寫字母s.split(" "); //按某個字符(比如空格)將字符串提取出來StringBuffer sb = new StringBuffer(); //用作字符串緩存器StringBuilder sb = new StringBuilder(); //與StringBuffer相似,沒有實現線程安全功能sb.append(""); //增加一個字符串sb.insert(n, ""); //插入字符串sb.toString(); //轉換成字符串String.format("Hello, %s. Next year, you‘ll be %d", name, age); //格式化字符串
//Java中的包裝類Integer類的常用方法:byteValue() //將Integer轉換為byte類型doubleValue() //將Integer轉換為double類型...基本類型轉換為字符串:Integer.toString(int n) //轉換為字符串類型
String.valueOf(int n) //int類型轉換為字符串類型
字符串轉換為基本類型:Integer.parseInt(Stirng s) //將字符串轉換為int類型Integer.valueOf(String s) //將字符串轉換為Integer類型
//時間類Date d = new Date(); //java.util包中,默認無參構建當前時間SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //java.text包中,創建目標格式對象String today = sdf.format(d); //將時間格式化為指定格式Date date = sdf.parse(today); //將字符串按指定格式轉化為時間-------Calendar c = Calendar.getInstance(); //java.util.Calendar,創建Calendar對象,初始化為當前時間int year = c.get(Calendar.YEAR);int month = c.get(Calendar.MONTH)+1; //0表示1月份int day = c.get(Calendar.DAY_OF_MONTH);int hour = c.get(Calendar.HOUR_OF_DAY);int minute = c.get(Calendar.MINUTE);int second = c.get(Calendar.SECOND);Date date = c.getTime(); //Date和Calendar類互轉Long time = c.getTimeInMillis(); //獲取當前毫秒數
//數學函數Math.abs(a); // java.lang包,求絕對值Math.pow(a); //指數運算Math.random(); //產生從0到1間的隨機數Math.round(a); //求近似Math.floor(); //返回小於參數的最大整數Math.ceil(); //返回大於參數的最小整數
//集合//容器類
Collection接口,子接口有Set,List,Queue,實現類有ArrayList, LinkedList, HashSetMap接口,實現類有HashMap---------ArrayList<String> list = new ArrayList<String>(); //定義ArrayList容器,ArrayList是List接口的一個實現類,List是Collection的子接口list.add(s); //增加元素list.add(n, s); //在位置n上插入元素list.addAll(Array.asList(array)); //將數組轉換為list並添加到list中list.addAll(n, Array.asList(array)); //插入到指定位置list.remove(s); //刪除指定元素或指定位置元素list.removeAll(Array.asList(array)); //刪除list中指定的元素集合list.set(n, s); //修改n位置的元素為slist.size(); //返回大小Iterator it = list.iterator(); //獲取list的叠代器it.hasNext(); //判斷叠代器是否有下一個元素it.Next(); //利用叠代器獲取下一個元素
//HashMap散列表HashMap<Integer, String> coinnames = new HashMap<Integer, String>();coinnames.put(1, "penny"); //放進鍵值對coinnames.get(1); //返回鍵1對應的值coinnames.containsKey(1); //是否包含某鍵coinnames.keySet(); //返回鍵的集合
//圖形界面包import javax.swing.JFrame; //圖形窗口import javax.swing.JPanel; //圖形面板import javax.swing.JButton; //圖形按鈕
import java.awt.Color; //顏色設置import java.awt.Graphics; //繪制圖像import java.awt.BorderLayout; //組件布局管理import java.awt.event.ActionEvent; //事件處理import java.awt.event.ActionListener; //事件監聽import javax.swing.JScrollPane; //滾動面板import javax.swing.JTable; //圖形表格import javax.swing.table.TableModel; //表格的數據模型接口

//圖片格式轉換、改變大小
  1. package com.qyluo.tmall.utils;
  2. import javax.imageio.ImageIO;
  3. import java.awt.*;
  4. import java.awt.image.*;
  5. import java.io.File;
  6. import java.io.IOException;
  7. /**
  8. * Created by qy_lu on 2017/5/10.
  9. */
  10. public class ImageUtil {
  11. public static BufferedImage change2jpg(File f) {
  12. try {
  13. java.awt.Image i = Toolkit.getDefaultToolkit().createImage(f.getAbsolutePath());
  14. PixelGrabber pg = new PixelGrabber(i, 0, 0, -1, -1, true);
  15. pg.grabPixels();
  16. int width = pg.getWidth(), height = pg.getHeight();
  17. final int[] RGB_MASKS = { 0xFF0000, 0xFF00, 0xFF };
  18. final ColorModel RGB_OPAQUE = new DirectColorModel(32, RGB_MASKS[0], RGB_MASKS[1], RGB_MASKS[2]);
  19. DataBuffer buffer = new DataBufferInt((int[]) pg.getPixels(), pg.getWidth() * pg.getHeight());
  20. WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, RGB_MASKS, null);
  21. BufferedImage img = new BufferedImage(RGB_OPAQUE, raster, false, null);
  22. return img;
  23. } catch (InterruptedException e) {
  24. // TODO Auto-generated catch block
  25. e.printStackTrace();
  26. return null;
  27. }
  28. }
  29. public static void resizeImage(File srcFile, int width,int height, File destFile) {
  30. try {
  31. Image i = ImageIO.read(srcFile);
  32. i = resizeImage(i, width, height);
  33. ImageIO.write((RenderedImage) i, "jpg", destFile);
  34. } catch (IOException e) {
  35. // TODO Auto-generated catch block
  36. e.printStackTrace();
  37. }
  38. }
  39. public static Image resizeImage(Image srcImage, int width, int height) {
  40. try {
  41. BufferedImage buffImg = null;
  42. buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  43. buffImg.getGraphics().drawImage(srcImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
  44. return buffImg;
  45. } catch (Exception e) {
  46. // TODO Auto-generated catch block
  47. e.printStackTrace();
  48. }
  49. return null;
  50. }
  51. }

//上傳文件解析相關包
  1. import org.apache.commons.fileupload.FileItem;
  2. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  3. import org.apache.commons.fileupload.servlet.ServletFileUpload;

  1. public InputStream parseUpload(HttpServletRequest request, Map<String, String> params) {
  2. InputStream is = null;
  3. try {
  4. DiskFileItemFactory factory = new DiskFileItemFactory();
  5. ServletFileUpload upload = new ServletFileUpload(factory);
  6. factory.setSizeThreshold(1024 * 10240);
  7. List items = upload.parseRequest(request);
  8. Iterator iterator = items.iterator();
  9. while (iterator.hasNext()) {
  10. FileItem item = (FileItem) iterator.next();
  11. if (!item.isFormField()) {
  12. is = item.getInputStream();
  13. } else {
  14. String paramName = item.getFieldName();
  15. String paramValue = item.getString();
  16. paramValue = new String(paramValue.getBytes("ISO-8859-1"), "UTF-8");
  17. params.put(paramName, paramValue);
  18. }
  19. }
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. return is;
  24. }





學習筆記--Java