java获取ip地址(详解java编程思想)


在项目中一般都会遇到文件的读写,

一般有两个问题要进行处理

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. }

有不对的地方欢迎指出,谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发表评论

登录后才能评论