在java中,为什麽文件写之后,紧跟着就读,读取不到

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在java中,为什麽文件写之后,紧跟着就读,读取不到相关的知识,希望对你有一定的参考价值。

参考技术A 先看看你要写的文件存在还是不存在,如果存在的话,看是不是把想写的东西写进去了,如果写进去了再看为什么没有读取出来。一步一步用排除法解决问题,这也是解决编程过程中解决问题的基本思路。 参考技术B

这样可以

import java.io.*;

public class temp 

public static void main(String[] args) throws IOException 
PrintWriter p = new PrintWriter(new BufferedOutputStream(new FileOutputStream("a.txt")));
p.print("this is a test");
p.close();
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));
String s = r.readLine();
System.out.println(s);
r.close();


参考技术C 你写数据到文件的输出流是不是没有关闭就直接用输入流去读了?
输出流不关闭的话是内容是不会写到文件上的,一定要先关闭输出流,文件才写入完成

java web工程中读取properties文件,路径一直不知道怎么写

1. 使用java.lang.Class类的getResourceAsStream(String name)方法

InputStream in = getClass().getResourceAsStream("/config.properties");

在静态方法中,由于不能使用getClass()方法,必须写出类的名字。区别不大。

MyClass.class.getResourceAsStream("/config.properties");

使用这个方法,路径前面可以加斜杠也可以不加。根据Class类getResourceAsStream()方法的JavaDoc:

Finds a resource with a given name.  The rules for searching resources associated with a given class are implemented by the defining  class loader of the class.  This method delegates to this object's class loader.  If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream.  

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:  

If the name begins with a '/' ('\\u002f'), then the absolute name of the resource is the portion of the name following the '/'.  

Otherwise, the absolute name is of the following form:  

modified_package_name/name  

Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\\u002e'). 

就是说,这个path假如以斜杠开头,则斜杠后面的部分是文件相对classpath的路径;假如不是,Java会把这个path看作是“包名/文件名”的结构,会尝试在这个类的包里面去找,而不是从classpath开始找;在这种情况下,除非你把properties文件放到MyClass.class所属的包里面,不然都会是null的。

2. 使用java.lang.ClassLoader类的getResourceAsStream(String name)方法


路径是不能加斜杠的!非常重要。

MyClass.class.getClassLoader().getResourceAsStream("config.properties");

这是因为使用classloader进行读取,所输入的参数必须是一个相对classpath的绝对路径,在格式上,一个绝对路径是不能以'/'开头的。

注意这两个方法是同名的,但路径参数的格式截然不同。


3. 在Maven中的运用

现在几乎所有的web project都是maven project,Maven的默认设置是把

src/main/resources/

加入到classpath里面的。那么,最好的做法是把你的properties文件放进src/main/resources里面,然后用上面代码读取。用Class类的,一般要加斜杠;用ClassLoader类的,绝不能加斜杠!

假如是Eclipse里面,需要把这个src/main/resources加到classpath里面。具体操作是右击工程,选择“Configure buildpath”,根据Maven的要求,把src/main/java和src/main/resources都加进去,并且保证Exclude是none,Include是all,或者至少要包括你需要读取的文件。

参考技术A /**获取根目录下的mon.properties文件**/
public class PropertiesUtil
private Properties props=null;
public PropertiesUtil() throws IOException
props=new Properties();
props.load(new FileReader("mon.properties"));

参考技术B spring读取么?

以上是关于在java中,为什麽文件写之后,紧跟着就读,读取不到的主要内容,如果未能解决你的问题,请参考以下文章

JAVA中为什么要使用接口,继承接口不是还要重写方法吗?为什么不直接写那些方法呢?:::接口的最主要的作用是达到统一访问

为什麽要用ABAP动态内表

在Ubuntu下编写shell脚本读取文件

java 怎么用10个线程去读取文件夹里100个txt文件中的内容,读完之后同步写到一个文件中去。

java读取文件操作

为什麽我们一般会在自增列或交易时间列上建立聚集索引?