本文目錄一覽:
- 1、Java讀取文件的幾種方法
- 2、java 中配置文件路徑讀取使用FileReader和InputStream區別和用法?
- 3、java怎樣提取配置文件!怎麼才能採用ServletContext讀取
- 4、JAVA怎麼從配置文件中讀取數據
- 5、Java中spring讀取配置文件的幾種方法
- 6、如何讀取配置文件里的配置信息
Java讀取文件的幾種方法
方式一:採用ServletContext讀取,讀取配置文件的realpath,然後通過文件流讀取出來。因為是用ServletContext讀取文件路徑,所以配置文件可以放入在web-info的classes目錄中,也可以在應用層級及web-info的目錄中。文件存放位置具體在eclipse工程中的表現是:可以放在src下面,也可放在web-info及webroot下面等。因為是讀取出路徑後,用文件流進行讀取的,所以可以讀取任意的配置文件包括xml和properties。缺點:不能在servlet外面應用讀取配置信息。
方式二:採用ResourceBundle類讀取配置信息,
優點是:可以以完全限定類名的方式加載資源後,直接的讀取出來,且可以在非Web應用中讀取資源文件。缺點:只能加載類classes下面的資源文件且只能讀取.properties文件。
方式三:採用ClassLoader方式進行讀取配置信息
優點是:可以在非Web應用中讀取配置資源信息,可以讀取任意的資源文件信息
缺點:只能加載類classes下面的資源文件。
方法4 getResouceAsStream
XmlParserHandler.class.getResourceAsStream 與classloader不同
java 中配置文件路徑讀取使用FileReader和InputStream區別和用法?
一、按數據來源(去向)分類:
1、是文件: FileInputStream, FileOutputStream, FileReader, FileWriter
2、是byte[]:ByteArrayInputStream, ByteArrayOutputStream
3、是Char[]: CharArrayReader, CharArrayWriter
4、是String: StringBufferInputStream, StringReader, StringWriter
5、網絡數據流:InputStream, OutputStream, Reader, Writer
二、按是否格式化輸出分:
1、要格式化輸出:PrintStream, PrintWriter
三、按是否要緩衝分:
1、要緩衝:BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter
四、按數據格式分:
1、二進制格式(只要不能確定是純文本的): InputStream, OutputStream及其所有帶Stream結束的子類
2、純文本格式(含純英文與漢字或其他編碼方式);Reader, Writer及其所有帶Reader, Writer的子類
五、按輸入輸出分:
1、輸入:Reader, InputStream類型的子類
2、輸出:Writer, OutputStream類型的子類
六、特殊需要:
1、從Stream到Reader,Writer的轉換類:InputStreamReader, OutputStreamWriter
2、對象輸入輸出:ObjectInputStream, ObjectOutputStream
3、進程間通信:PipeInputStream, PipeOutputStream, PipeReader, PipeWriter
4、合併輸入:SequenceInputStream
5、更特殊的需要:PushbackInputStream, PushbackReader, LineNumberInputStream, LineNumberReader
決定使用哪個類以及它的構造進程的一般準則如下(不考慮特殊需要):
首先,考慮最原始的數據格式是什麼: 原則四
第二,是輸入還是輸出:原則五
第三,是否需要轉換流:原則六第1點
第四,數據來源(去向)是什麼:原則一
第五,是否要緩衝:原則三 (特別註明:一定要注意的是readLine()是否有定義,有什麼比read, write更特殊的輸入或輸出方法)
第六,是否要格式化輸出:原則二
Java中Inputstream與Reader的區別
Reader支持16位的Unicode字符輸出,InputStream支持8位的字符輸出。
Reader和InputStream分別是I/O庫提供的兩套平行獨立的等級機構,
InputStream、OutputStream是用來處理8位元的流,
Reader、Writer是用來處理16位元的流。
而在JAVA語言中,byte類型是8位的,char類型是16位的,所以在處理中文的時候需要用Reader和Writer。
值得說明的是,在這兩種等級機構下,還有一道橋樑InputStreamReader、OutputStreamWriter負責進行InputStream到Reader的適配和由OutputStream到Writer的適配。
java.io.Reader 和 java.io.InputStream 組成了 Java輸入類。Reader 用於讀入16位字符,也就是 Unicode編碼的字符;而 InputStream 用於讀入 ASCII字符和二進制數據。
在 Java中,有不同類型的 Reader 輸入流對應於不同的數據源:
FileReader 用於從文件輸入;
CharArrayReader 用於從程序中的字符數組輸入;
StringReader 用於從程序中的字符串輸入;
PipedReader 用於讀取從另一個線程中的 PipedWriter 寫入管道的數據。
相應的也有不同類型的 InputStream 輸入流對應於不同的數據源:FileInputStream,ByteArrayInputStream,StringBufferInputStream,PipedInputStream。另外,還有兩種沒有對應 Reader 類型的 InputStream 輸入流:
Socket 用於套接字;
URLConnection 用於 URL 連接。
這兩個類使用 getInputStream() 來讀取數據。
相應的,java.io.Writer 和 java.io.OutputStream 也有類似的區別。
java怎樣提取配置文件!怎麼才能採用ServletContext讀取
創建配置文件:
1、在項目的任意地方,右鍵-》New-》File-》FileName-》輸入-》名稱.properties(比如:config.properties)
2、訪問路徑:從根目錄開始出發(WebRoot)-WEB-INF-classes-config.properties,(如果有包名,在classes-包名-config.properties)(路徑可以直接從本地中項目的路徑,找到WEB-INF直接從地址中copy(比如我的本地磁盤保存是這樣的:F:\課程\s2課程\s2書上內容\Java Web\ServletTest\WebRoot\WEB-INF\classes\config.properties))
response.setContentType(“text/html”);
response.setCharacterEncoding(“utf-8”);
request.setCharacterEncoding(“utf-8”);
PrintWriter out = response.getWriter();
/************************使用servletContext.getResourceAsStream**************************************/
//實例化ServletContext
ServletContext servletContext=this.getServletContext();
// //獲取輸入流
// InputStream in=servletContext.getResourceAsStream(“\\WEB-INF\\classes\\config.properties”);
// Properties p=new Properties();
// //類的裝載
// p.load(in);
// //拿到配置文件中userName參數
// out.println(p.getProperty(“userName”));
/***************************普通的獲取配置文件**************************************/
String path= servletContext.getRealPath((“\\WEB-INF\\classes\\config.properties”));//拿到絕對路徑
FileInputStream in=new FileInputStream(path);
Properties p=new Properties();
p.load(in);
out.println(p.get(“userName”));
JAVA怎麼從配置文件中讀取數據
Java有個集合,叫Properties,去查下用法,挺好用的。可以將一個配置文件比如txt中的鍵值對讀入到集合中。
Java中spring讀取配置文件的幾種方法
Java中spring讀取配置文件的幾種方法如下:
一、讀取xml配置文件
(一)新建一個java bean
package chb.demo.vo;
public class HelloBean {
private String helloWorld;
public String getHelloWorld() {
return helloWorld;
}
public void setHelloWorld(String helloWorld) {
this.helloWorld = helloWorld;
}
}
(二)構造一個配置文件
?xml version=”1.0″ encoding=”UTF-8″?
!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN” “”
beans
bean id=”helloBean” class=”chb.demo.vo.HelloBean”
property name=”helloWorld”
valueHello!chb!/value
/property
/bean
/beans
(三)讀取xml文件
1.利用ClassPathXmlApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext(“beanConfig.xml”);
//這種用法不夠靈活,不建議使用。
HelloBean helloBean = (HelloBean)context.getBean(“helloBean”);
System.out.println(helloBean.getHelloWorld());
2.利用FileSystemResource讀取
Resource rs = new FileSystemResource(“D:/software/tomcat/webapps/springWebDemo/WEB-INF/classes/beanConfig.xml”);
BeanFactory factory = new XmlBeanFactory(rs);
HelloBean helloBean = (HelloBean)factory.getBean(“helloBean”);
System.out.println(helloBean.getHelloWorld());
值得注意的是:利用FileSystemResource,則配置文件必須放在project直接目錄下,或者寫明絕對路徑,否則就會拋出找不到文件的異常。
二、讀取properties配置文件
這裡介紹兩種技術:利用spring讀取properties 文件和利用java.util.Properties讀取
(一)利用spring讀取properties 文件
我們還利用上面的HelloBean.java文件,構造如下beanConfig.properties文件:
helloBean.class=chb.demo.vo.HelloBean
helloBean.helloWorld=Hello!chb!
屬性文件中的”helloBean”名稱即是Bean的別名設定,.class用於指定類來源。
然後利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader來讀取屬性文件
BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource(“beanConfig.properties”));
BeanFactory factory = (BeanFactory)reg;
HelloBean helloBean = (HelloBean)factory.getBean(“helloBean”);
System.out.println(helloBean.getHelloWorld());
(二)利用java.util.Properties讀取屬性文件
比如,我們構造一個ipConfig.properties來保存服務器ip地址和端口,如:
ip=192.168.0.1
port=8080
則,我們可以用如下程序來獲得服務器配置信息:
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(“ipConfig.properties”);
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println(“ip:”+p.getProperty(“ip”)+”,port:”+p.getProperty(“port”));
三 、用接口類WebApplicationContext來取。
private WebApplicationContext wac;
wac =WebApplicationContextUtils.getRequiredWebApplicationContext(
this.getServletContext());
wac = WebApplicationContextUtils.getWebApplicationContext(
this.getServletContext());
JdbcTemplate jdbcTemplate = (JdbcTemplate)ctx.getBean(“jdbcTemplate”);
其中,jdbcTemplate為spring配置文件中的一個bean的id值。
這種用法比較靈活,spring配置文件在web中配置啟動後,該類會自動去找對應的bean,而不用再去指定配置文件的具體位置。
如何讀取配置文件里的配置信息
以JAVA為例:
讀取配置文件中數據的具體方法:
1、先在項目中創建一個包(如:config),再創建一個配置文件(如:a.properties),添加配置信息如下:
比如:
name=kaka
age=28
代碼如下:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertyTest {
public static void main(String[] args) {
PropertyTest loadProp = new PropertyTest();
InputStream in = loadProp.getClass().getResourceAsStream(“/config/a.properties”);
Properties prop = new Properties();
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(prop.getProperty(“name”));
System.out.println(prop.getProperty(“age”));
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/280760.html