Test.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package com.uas.eis.serviceImpl;
  2. import java.io.*;
  3. /**
  4. * @author: zhouy
  5. * @date: 2021/11/26 16:10
  6. * @desc:
  7. */
  8. public class Test {
  9. public static void main(String[] args) throws Exception {
  10. InputStream is=new FileInputStream("C:\\Picture\\a.txt");
  11. InputStreamReader isr = new InputStreamReader(is);
  12. BufferedReader br = new BufferedReader(isr);
  13. String str = null;
  14. StringBuilder sb = new StringBuilder();
  15. while ((str = br.readLine()) != null) {
  16. System.out.println(str);
  17. sb.append(str);
  18. }
  19. saveToImgFile(sb.toString().toUpperCase(),"C:\\Picture\\p7.jpg");
  20. /* DataOutputStream sos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("C:\\\\Picture\\\\p5.jpg")));
  21. InputStream in = new FileInputStream("C:\\Picture\\a.txt");
  22. int len = 0;
  23. byte[] b = new byte[1024];
  24. while ((len = in.read(b)) != -1) {
  25. System.out.println("write");
  26. sos.write(b,0,len);
  27. }
  28. sos.close();
  29. in.close();*/
  30. }
  31. public static void saveToImgFile(String src,String output){
  32. if(src==null||src.length()==0){
  33. return;
  34. }
  35. try{
  36. FileOutputStream out = new FileOutputStream(new File(output));
  37. byte[] bytes = src.getBytes();
  38. for(int i=0;i<bytes.length;i+=2){
  39. out.write(charToInt(bytes[i])*16+charToInt(bytes[i+1]));
  40. }
  41. out.close();
  42. }catch(Exception e){
  43. e.printStackTrace();
  44. }
  45. }
  46. private static int charToInt(byte ch){
  47. int val = 0;
  48. if(ch>=0x30&&ch<=0x39){
  49. val=ch-0x30;
  50. }else if(ch>=0x41&&ch<=0x46){
  51. val=ch-0x41+10;
  52. }
  53. return val;
  54. }
  55. }