资讯

精准传达 • 有效沟通

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

文件读写java代码,文件的读写java

JAVA文件的读写

public class ReadFromFile {

创新互联建站"三网合一"的企业建站思路。企业可建设拥有电脑版、微信版、手机版的企业网站。实现跨屏营销,产品发布一步更新,电脑网络+移动网络一网打尽,满足企业的营销需求!创新互联建站具备承接各种类型的做网站、网站建设项目的能力。经过10年的努力的开拓,为不同行业的企事业单位提供了优质的服务,并获得了客户的一致好评。

/**

* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。

*/

public static void readFileByBytes(String fileName) {

File file = new File(fileName);

InputStream in = null;

try {

System.out.println("以字节为单位读取文件内容,一次读一个字节:");

// 一次读一个字节

in = new FileInputStream(file);

int tempbyte;

while ((tempbyte = in.read()) != -1) {

System.out.write(tempbyte);

}

in.close();

} catch (IOException e) {

e.printStackTrace();

return;

}

try {

System.out.println("以字节为单位读取文件内容,一次读多个字节:");

// 一次读多个字节

byte[] tempbytes = new byte[100];

int byteread = 0;

in = new FileInputStream(fileName);

ReadFromFile.showAvailableBytes(in);

// 读入多个字节到字节数组中,byteread为一次读入的字节数

while ((byteread = in.read(tempbytes)) != -1) {

System.out.write(tempbytes, 0, byteread);

}

} catch (Exception e1) {

e1.printStackTrace();

} finally {

if (in != null) {

try {

in.close();

} catch (IOException e1) {

}

}

}

}

/**

* 以字符为单位读取文件,常用于读文本,数字等类型的文件

*/

public static void readFileByChars(String fileName) {

File file = new File(fileName);

Reader reader = null;

try {

System.out.println("以字符为单位读取文件内容,一次读一个字节:");

// 一次读一个字符

reader = new InputStreamReader(new FileInputStream(file));

int tempchar;

while ((tempchar = reader.read()) != -1) {

// 对于windows下,\r\n这两个字符在一起时,表示一个换行。

// 但如果这两个字符分开显示时,会换两次行。

// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。

if (((char) tempchar) != '\r') {

System.out.print((char) tempchar);

}

}

reader.close();

} catch (Exception e) {

e.printStackTrace();

}

try {

System.out.println("以字符为单位读取文件内容,一次读多个字节:");

// 一次读多个字符

char[] tempchars = new char[30];

int charread = 0;

reader = new InputStreamReader(new FileInputStream(fileName));

// 读入多个字符到字符数组中,charread为一次读取字符数

while ((charread = reader.read(tempchars)) != -1) {

// 同样屏蔽掉\r不显示

if ((charread == tempchars.length)

(tempchars[tempchars.length - 1] != '\r')) {

System.out.print(tempchars);

} else {

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

if (tempchars[i] == '\r') {

continue;

} else {

System.out.print(tempchars[i]);

}

}

}

}

} catch (Exception e1) {

e1.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e1) {

}

}

}

}

/**

* 以行为单位读取文件,常用于读面向行的格式化文件

*/

public static void readFileByLines(String fileName) {

File file = new File(fileName);

BufferedReader reader = null;

try {

System.out.println("以行为单位读取文件内容,一次读一整行:");

reader = new BufferedReader(new FileReader(file));

String tempString = null;

int line = 1;

// 一次读入一行,直到读入null为文件结束

while ((tempString = reader.readLine()) != null) {

// 显示行号

System.out.println("line " + line + ": " + tempString);

line++;

}

reader.close();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e1) {

}

}

}

}

/**

* 随机读取文件内容

*/

public static void readFileByRandomAccess(String fileName) {

RandomAccessFile randomFile = null;

try {

System.out.println("随机读取一段文件内容:");

// 打开一个随机访问文件流,按只读方式

randomFile = new RandomAccessFile(fileName, "r");

// 文件长度,字节数

long fileLength = randomFile.length();

// 读文件的起始位置

int beginIndex = (fileLength 4) ? 4 : 0;

// 将读文件的开始位置移到beginIndex位置。

randomFile.seek(beginIndex);

byte[] bytes = new byte[10];

int byteread = 0;

// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。

// 将一次读取的字节数赋给byteread

while ((byteread = randomFile.read(bytes)) != -1) {

System.out.write(bytes, 0, byteread);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

if (randomFile != null) {

try {

randomFile.close();

} catch (IOException e1) {

}

}

}

}

/**

* 显示输入流中还剩的字节数

*/

private static void showAvailableBytes(InputStream in) {

try {

System.out.println("当前字节输入流中的字节数为:" + in.available());

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

String fileName = "C:/temp/newTemp.txt";

ReadFromFile.readFileByBytes(fileName);

ReadFromFile.readFileByChars(fileName);

ReadFromFile.readFileByLines(fileName);

ReadFromFile.readFileByRandomAccess(fileName);

}

}

java文件读写

写入的时候,你先读一下之前的文件,如果有内容,就在原有的基础上+"\r\n"+加上要覆盖的内容。

如果没有,直接写入就可以了。

从文件中读取图片和写入图片到文件里的java代码是什么?

首先导入各种需要的包:\x0d\x0aimport java.awt.Image;\x0d\x0aimport javax.imageio.ImageIO;\x0d\x0aimport java.io.*;\x0d\x0a读取图片的方法如下:\x0d\x0aImage[] array = new Image[10];\x0d\x0aImage image = ImageIO.read(new File("d:\\source.gif"));//根据你实际情况改文件路径吧\x0d\x0aarray[0] = image;\x0d\x0a图片读出来了。\x0d\x0a\x0d\x0a如果你有一个Image对象,想把它写入文件可以这样做:\x0d\x0aBufferedImage image = ImageIO.read(new File("d:\\source.gif"));\x0d\x0a//要想保存这个对象的话你要把image声明为BufferedImage 类型\x0d\x0aImageIO.write(image, "png", new File("f:\\test.png"));

求用java读写properties文件的代码

Java代码

package com.LY;

import java.io.BufferedInputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Enumeration;

import java.util.Properties;

public class TestMain {

// 根据key读取value

public static String readValue(String filePath, String key) {

Properties props = new Properties();

try {

InputStream in = new BufferedInputStream(new FileInputStream(

filePath));

props.load(in);

String value = props.getProperty(key);

System.out.println(key + value);

return value;

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

// 读取properties的全部信息

public static void readProperties(String filePath) {

Properties props = new Properties();

try {

InputStream in = new BufferedInputStream(new FileInputStream(

filePath));

props.load(in);

Enumeration en = props.propertyNames();

while (en.hasMoreElements()) {

String key = (String) en.nextElement();

String Property = props.getProperty(key);

System.out.println(key + Property);

}

} catch (Exception e) {

e.printStackTrace();

}

}

// 写入properties信息

public static void writeProperties(String filePath, String parameterName,

String parameterValue) {

Properties prop = new Properties();

try {

InputStream fis = new FileInputStream(filePath);

// 从输入流中读取属性列表(键和元素对)

prop.load(fis);

// 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。

// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。

OutputStream fos = new FileOutputStream(filePath);

prop.setProperty(parameterName, parameterValue);

// 以适合使用 load 方法加载到 Properties表中的格式,

// 将此 Properties 表中的属性列表(键和元素对)写入输出流

prop.store(fos, "Update '" + parameterName+ "' value");

} catch (IOException e) {

System.err.println("Visit " + filePath + " for updating "

+ parameterName + " value error");

}

}

public static void main(String[] args) {

readValue("info.properties", "url");

writeProperties("info.properties", "age","22");

readProperties("info.properties");

System.out.println("OK");

}

}

Java如何读写txt文件的代码

有关Java如何读写txt文件这个问题经常在面试时会被问到,不懂或不熟悉的同志们可是要记好了哟!先来看下具体实现吧! package common; import java.io.*; import java.util.ArrayList; public class IOTest { public static void main (String args[]) { ReadDate(); WriteDate(); } /** * 读取数据 */ public static void ReadDate() { String url = “e:/2.txt”; try { FileReader read = new FileReader(new File(url)); StringBuffer sb = new StringBuffer(); char ch[] = new char[1024]; int d = read.read(ch); while(d!=-1){ String str = new String(ch,0,d); sb.append(str); d = read.read(ch); } System.out.print(sb.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 写入数据 */ public static void WriteDate() { try{ File file = new File(“D:/abc.txt”); if (file.exists()) { file.delete(); } file.createNewFile(); BufferedWriter output = new BufferedWriter(new FileWriter(file)); ArrayList ResolveList = new ArrayList(); for (int i = 0; i 10; i++) { ResolveList.add(Math.random()* 100); } for (int i=0 ;i output.write(String.valueOf(ResolveList.get(i)) + “\n”); } output.close(); } catch (Exception ex) { System.out.println(ex); } } }

Java文件读写

实用的模糊(通配符)文件查找程序

1 import java.io.File;

2 import java.util.regex.Matcher;

3 import java.util.regex.Pattern;

4 import java.util.ArrayList;

5

6 /** *//**

7 * pTitle: FileService /p 8* pDescription: 获取文件 /p 9* pCopyright: Copyright (c) 2007/p

10* pCompany: /p

11* @author not attributable

12* @version 1.0

13*/

14public class FileService {

15 public FileService() {

16 }

17

18 /** *//**

19 * 在本文件夹下查找

20 * @param s String 文件名

21 * @return File[] 找到的文件

22 */

23 public static File[] getFiles(String s)

24 {

25 return getFiles("./",s);

26 }

27

28 /** *//**

29 * 获取文件

30 * 可以根据正则表达式查找

31 * @param dir String 文件夹名称

32 * @param s String 查找文件名,可带*.?进行模糊查询

33 * @return File[] 找到的文件

34 */

35 public static File[] getFiles(String dir,String s) {

36 //开始的文件夹

37 File file = new File(dir);

38

39 s = s.replace('.', '#');

40 s = s.replaceAll("#", "\\\\.");


网站名称:文件读写java代码,文件的读写java
网站链接:http://cdkjz.cn/article/dsiiiis.html
多年建站经验

多一份参考,总有益处

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

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

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