如何一次性将整个js读取properties文件件里面的内容读取出来

查看: 2612|回复: 18|关注: 0
有没有可以一次性读取txt文件全部内容的办法
<h1 style="color:# 麦片财富积分
新手, 积分 42, 距离下一级还需 8 积分
关注者: 1
请问现在我有一份txt文件 里面文本文件 数据文件都有 我可以一次性有效读取吗?
Data1=importdata('AA.txt');
value=Data1.
Data1=importdata('AA.txt');
text=Data1.
目前是这么些的 但是 数据文件很多没有读完 文本文件很多读出来都是乱码 很奇怪
另外有一个问题是 txt文件有多少行目前不确定(其实写的程序是针对对很多txt的读取 所以行数不确定)不知道这个是不是影响因素之一?
论坛优秀回答者
关注者: 274
建议你用textscan读取,可以定义数据格式
importdata只是读取常见格式,或者做一些简单的格式分析,并不能读取复杂的文本格式
<h1 style="color:# 麦片财富积分
关注者: 1
另外问一下 请问用matlab把txt文件导入进来后 如果根据关键字找出相关信息 能将这部分信息高亮出来 然后txt保存出去么?pdf或者doc都可以?
<h1 style="color:# 麦片财富积分
关注者: 1
建议你用textscan读取,可以定义数据格式
importdata只是读取常见格式,或者做一些简单的格式分析,并不能 ...
谢过了 先试试看
<h1 style="color:# 麦片财富积分
关注者: 1
建议你用textscan读取,可以定义数据格式
importdata只是读取常见格式,或者做一些简单的格式分析,并不能 ...
我查了一下 textscan这样的话是不是必须手动调整读取的格式%d %s这样的?但是我的文本很长 不可能一个个给定啊。。。
论坛优秀回答者
关注者: 274
我查了一下 textscan这样的话是不是必须手动调整读取的格式%d %s这样的?但是我的文本很长 不可能一个个 ...
textscan可以按照指定格式重复读取的
难道你的文本没有任何的规律么?你可以发出来看看
<h1 style="color:# 麦片财富积分
关注者: 1
textscan可以按照指定格式重复读取的
难道你的文本没有任何的规律么?你可以发出来看看 ...
怎么发 直接回复么?其实就是类似于邮件啊这样的 有点长 另外时不时出来个日期什么的有数字 有符号
论坛优秀回答者
关注者: 274
怎么发 直接回复么?其实就是类似于邮件啊这样的 有点长 另外时不时出来个日期什么的有数字 有符号 ...
最好是附件
短的话可以可以直接放在帖子里
<h1 style="color:# 麦片财富积分
关注者: 1
最好是附件
短的话可以可以直接放在帖子里
您好 附件就是了
13:42 上传
点击文件名下载附件
1.51 KB, 下载次数: 20
<h1 style="color:# 麦片财富积分
关注者: 1
最好是附件
短的话可以可以直接放在帖子里
对了我还忘记说了 这些是pdf转化出来的txt
站长推荐 /1
MATLAB中文论坛是全球最大的 MATLAB & Simulink 中文社区。用户免费注册会员后,即可下载代码,讨论问题,请教资深用户及结识书籍作者。立即注册加入我们吧!
MATLAB官方社交平台
MATLAB中文论坛微社区随笔- 300&
&&&&&&&&&&&
读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):一、通过jdk提供的java.util.Properties类。此类继承自java.util.HashTable,即实现了Map接口,所以,可使用相应的方法来操作属性文件,但不建议使用像put、putAll这两个方法,因为put方法不仅允许存入String类型的value,还可以存入Object类型的。因此java.util.Properties类提供了getProperty()和setProperty()方法来操作属性文件,同时使用store或save(已过时)来保存属性值(把属性值写入.properties配置文件)。在使用之前,还需要加载属性文件,它提供了两个方法:load和loadFromXML。load有两个方法的重载:load(InputStream inStream)、load(Reader reader),所以,可根据不同的方式来加载属性文件。可根据不同的方式来获取InputStream,如:1、通过当前类加载器的getResourceAsStream方法获取
InputStream&inStream&=&TestProperties.class.getClassLoader().getResourceAsStream("test.properties");&&
2、从文件获取
InputStream&inStream&=&new&FileInputStream(new&File("filePath"));&&
3、也是通过类加载器来获取,和第一种一样
InputStream&in&=&ClassLoader.getSystemResourceAsStream("filePath");&&
4、在servlet中,还可以通过context来获取InputStream
InputStream&in&=&context.getResourceAsStream("filePath");&&
5、通过URL来获取
URL&url&=&new&URL("path");&&
InputStream&inStream&=&url.openStream();&&
读取方法如下:
Properties&prop&=&new&Properties();&&
prop.load(inStream);&&
String&key&=&prop.getProperty("username");&&
二、通过java.util.ResourceBundle类来读取,这种方式比使用Properties要方便一些。1、通过ResourceBundle.getBundle()静态方法来获取(ResourceBundle是一个抽象类),这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可。
ResourceBundle&resource&=&ResourceBundle.getBundle("com/mmq/test");
String&key&=&resource.getString("username");&&
2、从InputStream中读取,获取InputStream的方法和上面一样,不再赘述。
ResourceBundle&resource&=&new&PropertyResourceBundle(inStream);&&
& & 注意:在使用中遇到的最大的问题可能是配置文件的路径问题,如果配置文件入在当前类所在的包下,那么需要使用包名限定,如:test.properties入在com.mmq包下,则要使用com/mmq/test.properties(通过Properties来获取)或com/mmq/test(通过ResourceBundle来获取);属性文件在src根目录下,则直接使用test.properties或test即可。
这里主要是总结使用getResourceAsStream方法和InputStream流去读取properties文件,使用getResourceAsStream方法去读取properties文件时需要特别注意properties文件路径的写法
关于getClass().getClassLoader()
InputStream&&&is&&&=&&&getClass().getClassLoader().getResourceAsStream("helloworld.properties");中getClass()和getClassLoader()都是什么意思呀.getClass():取得当前对象所属的Class对象&&getClassLoader():取得该Class对象的类装载器类装载器负责从Java字符文件将字符流读入内存,并构造Class类对象,在你说的问题哪里,通过它可以得到一个文件的输入流getClass :public final Class getClass()Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.Returns:the object of type Class that represents the runtime class of the object.
getClassLoaderpublic ClassLoader getClassLoader()Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.If a security manager is present, and the caller&s class loader is not null and the caller&s class loader is not the same as or an ancestor of the class loader for the class whose class loader is requested, then this method calls the security manager&s checkPermission method with a RuntimePermission("getClassLoader") permission to ensure it&s ok to access the class loader for the class.
If this object represents a primitive type or void, null is returned.
Returns:the class loader that loaded the class or interface represented by this object.Throws:SecurityException - if a security manager exists and its checkPermission method denies access to the class loader for the class.See Also:ClassLoader, SecurityManager.checkPermission(java.security.Permission), RuntimePermissionClass.getClassLoader()的一个小陷阱:)昨天我的code总在Integer.class.getClassLoader().getResource("*********");这一句抛出空指针异常,定位为getClassLoader()返回null,查了一下jdk的文档,原来这里还有一个陷阱:jdk中关于getClassLoader()的描述:/**&&&&&* Returns the class loader for the class. Some implementations may use&&&&&* null to represent the bootstrap class loader. This method will return&&&&&* null in such implementations if this class was loaded by the bootstrap&&&&&* class loader.&&&&&*&&&&&* &p& If a security manager is present, and the caller's class loader is&&&&&* not null and the caller's class loader is not the same as or an ancestor of&&&&&* the class loader for the class whose class loader is requested, then&&&&&* this method calls the security manager's &code&checkPermission&/code&&&&&&* method with a &code&RuntimePermission("getClassLoader")&/code&&&&&&* permission to ensure it's ok to access the class loader for the class.&&&&&*&&&&&* &p&If this object&&&&&* represents a primitive type or void, null is returned......
上面的英文可以用下面的话来理解:
装载类的过程非常简单:查找类所在位置,并将找到的Java类的字节码装入内存,生成对应的Class对象。Java的类装载器专门用来实现这样的过程,JVM并不止有一个类装载器,事实上,如果你愿意的话,你可以让JVM拥有无数个类装载器,当然这除了测试JVM外,我想不出还有其他的用途。你应该已经发现到了这样一个问题,类装载器自身也是一个类,它也需要被装载到内存中来,那么这些类装载器由谁来装载呢,总得有个根吧?没错,确实存在这样的根,它就是神龙见首不见尾的Bootstrap ClassLoader. 为什么说它神龙见首不见尾呢,因为你根本无法在Java代码中抓住哪怕是它的一点点的尾巴,尽管你能时时刻刻体会到它的存在,因为java的运行环境所需要的所有类库,都由它来装载,而它本身是C++写的程序,可以独立运行,可以说是JVM的运行起点,伟大吧。在Bootstrap完成它的任务后,会生成一个AppClassLoader(实际上之前系统还会使用扩展类装载器ExtClassLoader,它用于装载Java运行环境扩展包中的类),这个类装载器才是我们经常使用的,可以调用ClassLoader.getSystemClassLoader() 来获得,我们假定程序中没有使用类装载器相关操作设定或者自定义新的类装载器,那么我们编写的所有java类通通会由它来装载,值得尊敬吧。AppClassLoader查找类的区域就是耳熟能详的Classpath,也是初学者必须跨过的门槛,有没有灵光一闪的感觉,我们按照它的类查找范围给它取名为类路径类装载器。还是先前假定的情况,当Java中出现新的类,AppClassLoader首先在类传递给它的父类类装载器,也就是Extion ClassLoader,询问它是否能够装载该类,如果能,那AppClassLoader就不干这活了,同样Extion ClassLoader在装载时,也会先问问它的父类装载器。我们可以看出类装载器实际上是一个树状的结构图,每个类装载器有自己的父亲,类装载器在装载类时,总是先让自己的父类装载器装载(多么尊敬长辈),如果父类装载器无法装载该类时,自己就会动手装载,如果它也装载不了,那么对不起,它会大喊一声:Exception,class not found。有必要提一句,当由直接使用类路径装载器装载类失败抛出的是NoClassDefFoundException异常。如果使用自定义的类装载器loadClass方法或者ClassLoader的findSystemClass方法装载类,如果你不去刻意改变,那么抛出的是ClassNotFoundException。
这里jdk告诉我们:如果一个类是通过bootstrap 载入的,那我们通过这个类去获得classloader的话,有些jdk的实现是会返回一个null的,比如说我用 new Object().getClass().getClassLoader()的话,会返回一个null,这样的话上面的代码就会出现NullPointer异常.所以保险起见我们最好还是使用我们自己写的类来获取classloader("this.getClass().getClassLoader()&),这样一来就不会有问题。
阅读(...) 评论()2017年10月 C/C++大版内专家分月排行榜第三2017年9月 C/C++大版内专家分月排行榜第三2017年6月 C/C++大版内专家分月排行榜第三2017年5月 C/C++大版内专家分月排行榜第三2017年4月 C/C++大版内专家分月排行榜第三2017年3月 C/C++大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。最近遇到一项需求,要求把properties文件中的内容读取出来供用户修改,修改完后需要再重新保存到properties文件中。很简单的需求吧,可问题是Properties是继承自HashTable的,直接通过keySet()、keys()或entrySet()方法对Properties中的元素进行遍历时取出来的内容顺序与properties文件中的顺序不一致,这是问题一;问题二是就算取出来的时候是有序的,保存到文件中时又是无序的了。
&&&&&&&& 当然,解决这两个问题的方法有很多。我最终采用的方法是自定义一个PropertiesUtil类,该类继承自Properties。PropertiesUtil提供一个返回由key按照存入顺序组成的List的方法,getKeyList(),这样问题一就解决了。那如何保证getKeyList()方法返回的就是有序的key组成的集合呢?我查看了一下Properties方法的源码,发现其setProperty()方法实际上就是调用了父类HashTable的put()方法,其次Properties在从文件中加载内容时是按照文件顺序进行读取,然后调用父类HashTable的put()方法进行储存。所以问题的解决办法就是PropertiesUtil持有一个私有的可以有序存储key的集合,然后重写父类的put()方法,在方法体中照常通过super.put()进行属性的存储,同时将key添加到存储key的集合中。
&&&&&&&& Properties提供有save()方法和store()方法可以将当前对象的内容存放到指定的输出流中,但它们的底层逻辑都是一样的。通过调用keys()方法获取一个Enumeration,然后对该Enumeration进行遍历,依次将对应的key和value写入到输出流中,所以要保证写入是有序的,就要保证遍历keys()返回的Enumeration时取出的元素key是有序的。所以解决方法是重写keys()方法,保证遍历返回的Enumeration时得到的key是有序的。完整代码如下:
import java.io.BufferedW
import java.io.FileInputS
import java.io.FileNotFoundE
import java.io.FileOutputS
import java.io.IOE
import java.io.InputS
import java.io.OutputS
import java.io.OutputStreamW
import java.util.ArrayL
import java.util.E
import java.util.L
import java.util.P
public class PropertiesUtil extends Properties {
private static final long serialVersionUID = 1L;
private List&Object& keyList = new ArrayList&Object&();
* 默认构造方法
public PropertiesUtil() {
* 从指定路径加载信息到Properties
* @param path
public PropertiesUtil(String path) {
InputStream is = new FileInputStream(path);
this.load(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("指定文件不存在!");
} catch (IOException e) {
e.printStackTrace();
* 重写put方法,按照property的存入顺序保存key到keyList,遇到重复的后者将覆盖前者。
public synchronized Object put(Object key, Object value) {
this.removeKeyIfExists(key);
keyList.add(key);
return super.put(key, value);
* 重写remove方法,删除属性时清除keyList中对应的key。
public synchronized Object remove(Object key) {
this.removeKeyIfExists(key);
return super.remove(key);
* keyList中存在指定的key时则将其删除
private void removeKeyIfExists(Object key) {
keyList.remove(key);
* 获取Properties中key的有序集合
public List&Object& getKeyList() {
return keyL
* 保存Properties到指定文件,默认使用UTF-8编码
* @param path 指定文件路径
public void store(String path) {
this.store(path, "UTF-8");
* 保存Properties到指定文件,并指定对应存放编码
* @param path 指定路径
* @param charset 文件编码
public void store(String path, String charset) {
if (path != null && !"".equals(path)) {
OutputStream os = new FileOutputStream(path);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, charset));
this.store(bw, null);
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("存储路径不能为空!");
* 重写keys方法,返回根据keyList适配的Enumeration,且保持HashTable keys()方法的原有语义,
* 每次都调用返回一个新的Enumeration对象,且和之前的不产生冲突
public synchronized Enumeration&Object& keys() {
return new EnumerationAdapter&Object&(keyList);
* List到Enumeration的适配器
private class EnumerationAdapter&T& implements Enumeration&T& {
private int index = 0;
private final List&T&
private final boolean isE
public EnumerationAdapter(List&T& list) {
this.list =
this.isEmpty = list.isEmpty();
public boolean hasMoreElements() {
//isEmpty的引入是为了更贴近HashTable原有的语义,在HashTable中添加元素前调用其keys()方法获得一个Enumeration的引用,
//之后往HashTable中添加数据后,调用之前获取到的Enumeration的hasMoreElements()将返回false,但如果此时重新获取一个
//Enumeration的引用,则新Enumeration的hasMoreElements()将返回true,而且之后对HashTable数据的增、删、改都是可以在
//nextElement中获取到的。
return !isEmpty && index & list.size();
public T nextElement() {
if (this.hasMoreElements()) {
return list.get(index++);
return null;
阅读(...) 评论()

我要回帖

更多关于 js读取properties文件 的文章

 

随机推荐