詳解java編程思想「java獲取ip地址」

在項目中一般都會遇到文件的讀寫,

一般有兩個問題要進行處理

1路徑問題

2讀寫問題

路徑的解決方法

路徑之間的連接”//”=”\”=”/”

eg1:D盤下面file文件夾裡面的1.txt

path=”D://file//1.txt”

path=”D:/file/1.txt”

path=”D:\file\1.txt”

這三種都可以

1絕對路徑(堅決不推薦使用)

就是從電腦的根目錄開始C盤D盤,詳情參考eg1

2相對路徑

java項目中默認是從項目的根目錄開始的 如下圖

java項目中獲取路徑以及讀寫文件

獲取到該目錄下的所有文件(獲取的是一個目錄)

./ 獲取到當前根目錄

  1. String path=”./”;
  2. File f=new File(path);
  3. File[] files=f.listFiles();
  4. for(int i=0;i<files.length;i++){
  5. System.out.println(files[i].getName());
  6. }

../ 獲取到根目錄下的父目錄 想要獲取到多級的父目錄只需要../ 寫n個就好了(需要注意的是這種方法最大只能獲取到 Windows盤下面的根目錄,就是最多只能獲取到 C盤 或者D盤,不可能和Linux 那種 /root/D)web 項目中

主要是分清楚 工作空間和發布空間就好了

比如當初使用struts2文件上傳的時候

定義接受文件的目錄

ServletContext servletContext = ServletActionContext.getServletContext();

String str=servletContext.getRealPath(“/files/”+fileFileName);

eclipse暫時出了點小問題等會再寫這個

讀寫文件(如果不正確歡迎積極指出,一起進步)

因為文件有不同的格式,就文本文件來說有utf-8 GBK 等等

建議使用位元組流 ( InputStream是所有位元組輸入流的祖先,而OutputStream是所有位元組輸出流的祖先)進行讀取而不是字元流( Reader是所有讀取字元串輸入流的祖先,而writer是所有輸出字元串的祖先)

其實就是內部一個使用byte[]實現,一個是用char[] 這個可以看一下 JDK的源碼就了解了

具體 字元流位元組流之間的區別請看轉載處

  1. http://blog.csdn.net/zxman660/article/details/7875799
  2. http://blog.csdn.net/cynhafa/article/details/6882061
  1. 讀寫文件
  2. package com.wzh.utils;
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. public class InOutFile {
  11. /**
  12. * @param file null、一個文件、一個文件目錄、
  13. * <pre>
  14. * fileToByte(null)=null
  15. * fileToByte(file)=null file>2G
  16. * fileToByte(文件目錄)=null
  17. * fileToByte(file)=byte[]
  18. * </pre>
  19. * @return null(文件不存在,null,文件目錄,文件太大>2G) byte[](轉換成功)
  20. */
  21. public byte[] fileToByte(File file) {
  22. byte[] buffer = null;
  23. if (file == null) {
  24. throw new IndexOutOfBoundsException();
  25. }
  26. if (file.isDirectory())
  27. return buffer;
  28. if (file.length() > Integer.MAX_VALUE)
  29. return buffer;
  30. if (file.isFile()) {
  31. int filelength = (int) file.length();
  32. InputStream inputStream = null;
  33. BufferedInputStream bufferedInputStream = null;
  34. OutputStream outputStream=null;
  35. BufferedOutputStream bufferedOutputStream=null;
  36. File outfile=new File(“files//out//”+file.getName());
  37. int n = 0;
  38. int off = 0;
  39. int length = 4096;
  40. try {
  41. if(!outfile.exists())
  42. outfile.createNewFile();
  43. inputStream = new FileInputStream(file);
  44. outputStream=new FileOutputStream(outfile, true);
  45. bufferedInputStream = new BufferedInputStream(inputStream);
  46. bufferedOutputStream=new BufferedOutputStream(outputStream);
  47. buffer = new byte[filelength];
  48. /*
  49. * 添加(length <= filelength – off) ? length : filelength – off)的比較
  50. * 為了防止讀到最後buffer 剩餘的長度沒有4096 裝不下那麼多會導致讀取不了IndexOutOfBoundsException()
  51. * 當filelength – off=0時表示文件讀取完畢但是read內部認為是其他線程佔用io導致堵塞並不會認為文件讀取完畢
  52. * 所以要添加上filelength – off>0
  53. */
  54. while ((filelength – off) > 0 && (n = bufferedInputStream.read(buffer, off,
  55. ((length <= filelength – off) ? length : filelength – off))) >= 0) {
  56. bufferedOutputStream.write(buffer, off, n);
  57. off += n;
  58. }
  59. }
  60. catch (Exception e) {
  61. }
  62. finally {
  63. closeInputStream(bufferedInputStream);
  64. closeInputStream(inputStream);
  65. closeOutputStream(bufferedOutputStream);
  66. closeOutputStream(outputStream);
  67. System.out.println(“end”);
  68. }
  69. }
  70. return buffer;
  71. }
  72. /**
  73. * close inoutstream
  74. * @param inputStream null or the inputstream’s child
  75. */
  76. private void closeInputStream(InputStream inputStream) {
  77. if (inputStream == null)
  78. return;
  79. try {
  80. inputStream.close();
  81. }
  82. catch (Exception e) {
  83. }
  84. }
  85. /**
  86. * close outputStream
  87. * @param outputStream null or the outputStream child
  88. */
  89. private void closeOutputStream(OutputStream outputStream) {
  90. if (outputStream == null)
  91. return;
  92. try {
  93. outputStream.flush();
  94. outputStream.close();
  95. }
  96. catch (Exception e) {
  97. }
  98. }
  99. }

當然字元流也可以進行讀取文件

字元流也可以進行讀取文件只不過要指定文件(文本文件)的編碼

  1. /**
  2. * 創建不同格式的文本文件
  3. * @throws Exception
  4. */
  5. private void createFile() throws Exception {
  6. File file = new File(“files//kindsformat//utf//1.txt”);
  7. //File file = new File(“files//kindsformat//gbk//1.txt”);
  8. if (!file.exists())
  9. file.createNewFile();
  10. Writer writer = new OutputStreamWriter(new FileOutputStream(file, true), “UTF-8”);
  11. //Writer writer = new OutputStreamWriter(new FileOutputStream(file), “GBK”);
  12. BufferedWriter bufferedWriter = new BufferedWriter(writer);
  13. bufferedWriter.write(“我是中文測試啊測試啊”);
  14. bufferedWriter.flush();
  15. bufferedWriter.close();
  16. writer.close();
  17. }

有不對的地方歡迎指出,謝謝

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/275378.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-17 14:21
下一篇 2024-12-17 14:21

相關推薦

發表回復

登錄後才能評論