RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
java读写文件代码解释 java文件读取和写入实例

java中写入文件时,这些代码是什么意思?尤其是那个exists()函数的意思

判断文件存在否,不存在创建,然后再写入些内容。

专业领域包括成都网站制作、成都网站建设、外贸营销网站建设商城网站建设、微信营销、系统平台开发, 与其他网站设计及系统开发公司不同,成都创新互联的整合解决方案结合了帮做网络品牌建设经验和互联网整合营销的理念,并将策略和执行紧密结合,为客户提供全网互联网整合方案。

这里的注释也比较清楚了,lz其实可以自己查看api文档,看看每个具体方法的作用,这样印象更深。

java读取、修改、写入txt文件

模拟:先创建一个TXT文件(内容来自控制台);然后读取文件并在控制台输出;最后实现对新创建的TXT文件(的数据进行排序后)的复制。分别对应三个函数,调用顺序需要注意:创建、读取、复制。

效果图如下:绿色部分为控制台输入的内容(当输入end时,结束)

代码如下:

package com.baidu;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.util.Arrays;

import java.util.Scanner;

import java.util.Vector;

public class CreateAndReadTxt {

// 文件名称

public static String fileName = ".txt";

public static String newFileName = ".txt";

// 文件路径

public final static String URL = System.getProperty("user.dir");

// CreateAndReadTxt.class.getResource("/").getPath();

// 创建TXT文件

public static void createTxtFile(String fName, String fileContent) {

// 创建文件

fileName = fName + fileName;

File file = new File(fileName);

// 可以更改

file.setWritable(true);

// 判断当前路径下是否存在同名文件

boolean isExist = file.exists();

if (isExist) {

// 文件存在,删除

file.delete();

}

// 写入文件

try {

// 文件写入对象

FileOutputStream fos = new FileOutputStream(file);

// 输入流写入----默认字符为GBK

OutputStreamWriter osw = new OutputStreamWriter(fos);

// 写入

osw.write(fileContent);

// 写入完毕后关闭

osw.close();

System.out.println("成功创建文件:\t"+fileName);

} catch (IOException e) {

System.out.println("写入文件失败:\t" + e.getMessage());

}

}

// 阅读文件

public static void readFile(String fileName) {

System.out.println("开始读取文件:\t" + fileName);

// 产生文件对象

File file = new File(fileName);

//

try {

// 字符读取

FileReader fr = new FileReader(file);

// 缓冲处理

BufferedReader br = new BufferedReader(fr);

String str = "";

while ((str = br.readLine()) != null) {

System.out.println(str);

}

// 关闭

br.close();

fr.close();

} catch (FileNotFoundException e) {

System.out.println("读取文件失败:\t" + e.getMessage());

} catch (IOException e) {

System.out.println("读取文件失败:\t" + e.getMessage());

}

}

// 文件复制

public static void copyFile(String fromFileName,String toFileName){

//读取文件

File file = new File(fromFileName);

try {

FileReader fr = new FileReader(file);

BufferedReader br = new BufferedReader(fr);

// 定义接收变量

VectorDouble vec = new VectorDouble();

String s = "";

while(null!=(s=br.readLine())){

vec.add(Double.parseDouble(s));

}

br.close();

fr.close();

// 保存到数组并进行排序

Double dou[] = new Double[vec.size()];

vec.toArray(dou);

Arrays.sort(dou);

System.out.println("========复制文件=========");

// 写入新文件

newFileName = "副本"+newFileName;

File newFile = new File(toFileName);

FileOutputStream fos = new FileOutputStream(newFile, true);

OutputStreamWriter osm = new OutputStreamWriter(fos);

for(Double d:dou){

osm.write(d.doubleValue()+"\n");

}

osm.close();

fos.close();

} catch (FileNotFoundException e) {

System.out.println("读取文件失败:\t" + e.getMessage());

} catch (IOException e) {

System.out.println("读取文件失败:\t" + e.getMessage());

}

}

public static void main(String[] args) {

/**

 * 构造数据

 */

Scanner scan = new Scanner(System.in);

StringBuilder sb = new StringBuilder();

String s = "";

while(!("end".equals(s = scan.next()))){// 当输入end时,结束

sb.append(s);

sb.append("\n");

}

scan.close();

/**

 * 使用数据

 */

CreateAndReadTxt.createTxtFile("creat", sb.toString());

CreateAndReadTxt.readFile(fileName);

System.out.println(fileName);

CreateAndReadTxt.copyFile(fileName, newFileName);

CreateAndReadTxt.readFile(newFileName);

}

}

java远程读写文件详解

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

/**

* @author lmq

*

*/

public class RemoteFile {

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

File remoteFile = new File("//192.168.7.146/test/1.txt");// 192.168.7.146是对方机器IP,test是对方那个共享文件夹名字,如果没有共享是访问不到的

//远程文件其实主要是地址,地址弄对了就和本地文件没什么区别 ,windows里面//或者\\\\开头就表示这个文件是网络路径了其实这个地址就像我们再windows里面,点击开始

//然后点击运行,然后输入 \\192.168.7.146/test/1.txt访问远程文件一样的

BufferedReader br = new BufferedReader(new FileReader(remoteFile));

String str;

while ((str = br.readLine()) != null) {

System.out.println(str);

}

br.close();

}

}

希望能帮到你。

java读取文本文件代码

java读取文本文件的方法有很多 这个例子主要介绍最简单 最常用的BufferedReader类 完整例子如下 package net chinaunix blog hzm text;import java io BufferedReader;import java io FileReader;import java io IOException;public class ReadFile {private String path;public ReadFile(String filePath){path = filePath;}public String[] openFile() throws IOException{FileReader fr = new FileReader(path) BufferedReader textReader = new BufferedReader(fr) String[] textData = new String[readLines()];int i;for(i= ; i readLines() i++){textData[i] = textReader readLine() }textReader close() return textData;}int readLines() throws IOException{FileReader fileToRead = new FileReader(path) BufferedReader bf = new BufferedReader(fileToRead) int numberOfLines = ;@SuppressWarnings( unused )String oneLine;while((oneLine = bf readLine()) != null){numberOfLines++;}bf close() return numberOfLines;}}package net chinaunix blog hzm text;import java io IOException;public class FileData {public static void main(String[] args) throws IOException{String filePath = C:/text txt ;try{ReadFile reader = new ReadFile(filePath) String[] content = reader openFile() int i;for(i= ;icontent length;i++){System out println(content[i]) }}catch(IOException e){System out println( 异常信息 + e getMessage()) }}}java io BufferedReaderThe buffer size may be specified or the default size may be used The default is large enough for most purposes In general each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly such as FileReaders and InputStreamReaders For example BufferedReader in = new BufferedReader(new FileReader( foo in )) will buffer the input from the specified file Without buffering each invocation of read() or readLine() could cause bytes to be read from the file converted into characters and then returned which can be very inefficient Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader java io FileReaderFileReader is meant for reading streams of characters For reading streams of raw bytes consider using a FileInputStream lishixinzhi/Article/program/Java/hx/201311/26249


网站题目:java读写文件代码解释 java文件读取和写入实例
转载注明:http://scyingshan.cn/article/ddogcej.html