189 8069 5689

从文件中读取java代码 读取文件的代码java

java中怎样从文件中读取数据?

分为读字节,读字符两种读法\x0d\x0a◎◎◎FileInputStream 字节输入流读文件◎◎◎\x0d\x0apublic class Maintest {\x0d\x0a\x0d\x0apublic static void main(String[] args) throws IOException {\x0d\x0a\x0d\x0aFile f=new File("G:\\just for fun\\xiangwei.txt");\x0d\x0a\x0d\x0aFileInputStream fin=new FileInputStream(f);\x0d\x0a\x0d\x0abyte[] bs=new byte[1024];\x0d\x0a\x0d\x0aint count=0;\x0d\x0awhile((count=fin.read(bs))0)\x0d\x0a{\x0d\x0a\x0d\x0aString str=new String(bs,0,count);//反复定义新变量:每一次都 重新定义新变量,接收新读取的数据\x0d\x0a\x0d\x0aSystem.out.println(str);//反复输出新变量:每一次都 输出重新定义的新变量\x0d\x0a}\x0d\x0afin.close();\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a◎◎◎FileReader 字符输入流读文件◎◎◎\x0d\x0apublic class Maintest {\x0d\x0apublic static void main(String[] args) throws IOException {\x0d\x0a\x0d\x0aFile f=new File("H:\\just for fun\\xiangwei.txt");\x0d\x0a\x0d\x0aFileReader fre=new FileReader(f);\x0d\x0a\x0d\x0aBufferedReader bre=new BufferedReader(fre);\x0d\x0a\x0d\x0aString str="";\x0d\x0awhile((str=bre.readLine())!=null)//●判断最后一行不存在,为空\x0d\x0a{\x0d\x0aSystem.out.println(str);\x0d\x0a}\x0d\x0abre.close();\x0d\x0a fre.close();\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a}

10年积累的网站制作、成都做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有含山免费网站建设让你可以放心的选择与我们合作。

从文件中读取数据并编码成base64 java

代码如下:

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Arrays;

import java.util.Base64;

import java.util.Base64.Encoder;

public class App {

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

Encoder encoder = Base64.getEncoder();

byte[] buffer = new byte[1024];

int len = 0;

StringBuilder builder = new StringBuilder();

try (FileInputStream inputStream = new FileInputStream("d:\\temp\\abc.txt")) {

while ((len = inputStream.read(buffer)) != -1) {

byte[] src = Arrays.copyOfRange(buffer, 0, len);

builder.append(encoder.encodeToString(src));

}

}

System.out.println(builder.toString());

}

}

跪求Java中写入文件和从文件中读取数据的最佳的代码!

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class IOTest {

public static void main(String[] args) {

String str = "123\r\n456";

writeFile(str);//写

String str1 = readFile();//读

System.out.println(str1);

}

/**

* 传递写的内容

* @param str

*/

static void writeFile(String str) {

try {

File file = new File("d:\\file.txt");

if(file.exists()){//存在

file.delete();//删除再建

file.createNewFile();

}else{

file.createNewFile();//不存在直接创建

}

FileWriter fw = new FileWriter(file);//文件写IO

fw.write(str);

fw.flush();

fw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 返回读取的内容

* @return

*/

static String readFile() {

String str = "", temp = null;

try {

File file = new File("d:\\file.txt");

FileReader fr = new FileReader(file);

BufferedReader br = new BufferedReader(fr);//文件读IO

while((temp = br.readLine())!=null){//读到结束为止

str += (temp+"\n");

}

br.close();

fr.close();

} catch (IOException e) {

e.printStackTrace();

}

return str;

}

}

刚写的,够朋友好好学习一下啦,呵呵

多多看API,多多练习

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

首先导入各种需要的包:

import java.awt.Image;

import javax.imageio.ImageIO;

import java.io.*;

读取图片的方法如下:

Image[] array = new Image[10];

Image image = ImageIO.read(new File("d:\\source.gif"));//根据你实际情况改文件路径吧

array[0] = image;

图片读出来了。

如果你有一个Image对象,想把它写入文件可以这样做:

BufferedImage image = ImageIO.read(new File("d:\\source.gif"));

//要想保存这个对象的话你要把image声明为BufferedImage 类型

ImageIO.write(image, "png", new File("f:\\test.png"));

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中如何从文件中读取数据

分为读字节,读字符两种读法

◎◎◎FileInputStream 字节输入流读文件◎◎◎

public class Maintest {

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

File f=new File("G:\\just for fun\\xiangwei.txt");

FileInputStream fin=new FileInputStream(f);

byte[] bs=new byte[1024];

int count=0;

while((count=fin.read(bs))0)

{

String str=new String(bs,0,count); //反复定义新变量:每一次都 重新定义新变量,接收新读取的数据

System.out.println(str); //反复输出新变量:每一次都 输出重新定义的新变量

}

fin.close();

}

}

◎◎◎FileReader 字符输入流读文件◎◎◎

public class Maintest {

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

File f=new File("H:\\just for fun\\xiangwei.txt");

FileReader fre=new FileReader(f);

BufferedReader bre=new BufferedReader(fre);

String str="";

while((str=bre.readLine())!=null) //●判断最后一行不存在,为空

{

System.out.println(str);

}

bre.close();

fre.close();

}

}


文章标题:从文件中读取java代码 读取文件的代码java
当前URL:http://jkwzsj.com/article/hpjjhi.html

其他资讯