资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

用java代码读取文件,用java代码读取文件

用java如何读取linux中的某个文件

java是跨平台语言,在linux上读文件跟在windows上读文件是一样的 只是文件路径不一样,可以用File对象和FileInputSteam来读取。但要注意文件编码问题。

我们提供的服务有:网站制作、成都网站制作、微信公众号开发、网站优化、网站认证、独山子ssl等。为上1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的独山子网站制作公司

如果有中文请做适当的编码转换,通常情况下Linux的默认字符编码为UTF-8编码方式,项目可以直接采用utf8编码方式操作.用System.getProperty("file.encoding")可检查系统编码格式。可改操作系统的文件系统编码,vi /etc/profile,在文件末尾加上

export LANG="zh_CN.GBK"

export LC_ALL="zh_CN.GBK"

编码转换代码:new String(files[i].getName().getBytes("GBK"),"UTF-8");

文件操作的核心代码请参考下面代码:

String path= "/home/";

path= "/home/multiverse/Repository/PMEPGImport";

File file=new File(path);

File[] tempList = file.listFiles();

for (int i = 0; i tempList.length; i++) {

if (tempList[i].isFile()) {

//FileInputStream fis = new FileInputStream("fileName");

//InputStreamReader isr = new InputStreamReader(fis,"utf-8");

StringBuffer buffer = new StringBuffer();

String text;

BufferedReader input = new BufferedReader (new FileReader(tempList[i]));

while((text = input.readLine()) != null)

buffer.append(text +"/n"); }

if (tempList[i].isDirectory()) {

System.out.println("文件夹:"+tempList[i]);

}

}

怎么用java代码读取excel文件

本例使用java来读取excel的内容并展出出结果,代码如下:

复制代码 代码如下:

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.text.DecimalFormat;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Date;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ExcelOperate {

public static void main(String[] args) throws Exception {

File file = new File("ExcelDemo.xls");

String[][] result = getData(file, 1);

int rowLength = result.length;

for(int i=0;irowLength;i++) {

for(int j=0;jresult[i].length;j++) {

System.out.print(result[i][j]+"\t\t");

}

System.out.println();

}

}

/**

* 读取Excel的内容,第一维数组存储的是一行中格列的值,二维数组存储的是多少个行

* @param file 读取数据的源Excel

* @param ignoreRows 读取数据忽略的行数,比喻行头不需要读入 忽略的行数为1

* @return 读出的Excel中数据的内容

* @throws FileNotFoundException

* @throws IOException

*/

public static String[][] getData(File file, int ignoreRows)

throws FileNotFoundException, IOException {

ListString[] result = new ArrayListString[]();

int rowSize = 0;

BufferedInputStream in = new BufferedInputStream(new FileInputStream(

file));

// 打开HSSFWorkbook

POIFSFileSystem fs = new POIFSFileSystem(in);

HSSFWorkbook wb = new HSSFWorkbook(fs);

HSSFCell cell = null;

for (int sheetIndex = 0; sheetIndex wb.getNumberOfSheets(); sheetIndex++) {

HSSFSheet st = wb.getSheetAt(sheetIndex);

// 第一行为标题,不取

for (int rowIndex = ignoreRows; rowIndex = st.getLastRowNum(); rowIndex++) {

HSSFRow row = st.getRow(rowIndex);

if (row == null) {

continue;

}

int tempRowSize = row.getLastCellNum() + 1;

if (tempRowSize rowSize) {

rowSize = tempRowSize;

}

String[] values = new String[rowSize];

Arrays.fill(values, "");

boolean hasValue = false;

for (short columnIndex = 0; columnIndex = row.getLastCellNum(); columnIndex++) {

String value = "";

cell = row.getCell(columnIndex);

if (cell != null) {

// 注意:一定要设成这个,否则可能会出现乱码

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

switch (cell.getCellType()) {

case HSSFCell.CELL_TYPE_STRING:

value = cell.getStringCellValue();

break;

case HSSFCell.CELL_TYPE_NUMERIC:

if (HSSFDateUtil.isCellDateFormatted(cell)) {

Date date = cell.getDateCellValue();

if (date != null) {

value = new SimpleDateFormat("yyyy-MM-dd")

.format(date);

} else {

value = "";

}

} else {

value = new DecimalFormat("0").format(cell

.getNumericCellValue());

}

break;

case HSSFCell.CELL_TYPE_FORMULA:

// 导入时如果为公式生成的数据则无值

if (!cell.getStringCellValue().equals("")) {

value = cell.getStringCellValue();

} else {

value = cell.getNumericCellValue() + "";

}

break;

case HSSFCell.CELL_TYPE_BLANK:

break;

case HSSFCell.CELL_TYPE_ERROR:

value = "";

break;

case HSSFCell.CELL_TYPE_BOOLEAN:

value = (cell.getBooleanCellValue() == true ? "Y"

: "N");

break;

default:

value = "";

}

}

if (columnIndex == 0 value.trim().equals("")) {

break;

}

values[columnIndex] = rightTrim(value);

hasValue = true;

}

java中怎样从一个文件中读取文件信息

java读取文件路径、所占空间大小等文件消息,主要是使用FileInputStream类来操作,示例如下:

import java.io.File;

import java.io.FileInputStream;

public class ceshi {

public static void main(String[] args) throws Exception {

java.io.File localFile = new File("D:\\1.txt");

FileInputStream ins = new FileInputStream(localFile);

int countLen = ins.available();

byte[] m_binArray = new byte[countLen];

ins.read(m_binArray);

ins.close();

System.out.println(localFile.getAbsoluteFile() + "  "

+ localFile.getFreeSpace());

}

}

运行结果如下:

java 怎么读取配置文件

一.读取xml配置文件

(一)新建一个java bean(HelloBean. java)

java代码

(二)构造一个配置文件(beanConfig.xml)

xml 代码

(三)读取xml文件

1.利用ClassPathXmlApplicationContext

java代码

2.利用FileSystemResource读取

java代码

二.读取properties配置文件

这里介绍两种技术:利用spring读取properties 文件和利用java.util.Properties读取

(一)利用spring读取properties 文件

我们还利用上面的HelloBean. java文件,构造如下beanConfig.properties文件:

properties 代码

helloBean.class=chb.demo.vo.HelloBean

helloBean.helloWorld=Hello!chb!

属性文件中的"helloBean"名称即是Bean的别名设定,.class用于指定类来源。

然后利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件

java代码

(二)利用java.util.Properties读取属性文件

比如,我们构造一个ipConfig.properties来保存服务器ip地址和端口,如:

properties 代码

ip=192.168.0.1

port=8080

三.读取位于Jar包之外的properties配置文件

下面仅仅是列出读取文件的过程,剩下的解析成为properties的方法同上

1 FileInputStream reader = new FileInputStream("config.properties");

2 num = reader.read(byteStream);

3 ByteArrayInputStream inStream = new ByteArrayInputStream(byteStream, 0, num);

四.要读取的配置文件和类文件一起打包到一个Jar中

String currentJarPath = URLDecoder.decode(YourClassName.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8"); //获取当前Jar文件名,并对其解码,防止出现中文乱码

JarFile currentJar = new JarFile(currentJarPath);

JarEntry dbEntry = currentJar.getJarEntry("包名/配置文件");

InputStream in = currentJar.getInputStream(dbEntry);

//以上YourClassName是class全名,也就是包括包名

修改:

JarOutputStream out = new FileOutputStream(currentJarPath);

out.putNextEntry(dbEntry);

out.write(byte[] b, int off, int len); //写配置文件

。。。

out.close();


网站栏目:用java代码读取文件,用java代码读取文件
文章分享:http://cdkjz.cn/article/hdpiih.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220