网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java读取数据流代码 java 读取流

Java怎么读取图中的数据,而且不乱码?希望附带代码

不知道你的文件格式,不过你可以可以尝试用io流来读取。下面代码 我试过是可以读取挺多格式文件的,你试下 拷贝过去改下文件路径就行了。

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

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class ReadFile

{

public static void main(String[] args)

{

//文件位置

String filepath = "D:\\test.pub";

/** 一次读取所有内容 */

FileInputStreamReadFile(filepath);

System.out.println("=====================");

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

BufferedReaderReadFile(filepath);

System.out.println("=====================");

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

ReadFileByByte(filepath);

System.out.println("\n=====================");

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

InputSteamReaderReadFile(filepath);

System.out.println("\n=====================");

}

private static void InputSteamReaderReadFile(String filepath)

{

try

{

InputStreamReader sr = new InputStreamReader(new FileInputStream(new File(filepath)));

int temp = 0;

while ((temp = sr.read()) != -1)

{

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

}

sr.close();

}

catch (FileNotFoundException e)

{

e.printStackTrace();

}

catch (IOException e)

{

e.printStackTrace();

}

}

private static void ReadFileByByte(String filepath)

{

try

{

File file = new File(filepath);

FileInputStream fis = new FileInputStream(file);

int b = 0;

while ((b = fis.read()) != -1)

{

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

}

fis.close();

}

catch (FileNotFoundException e)

{

e.printStackTrace();

}

catch (IOException e)

{

e.printStackTrace();

}

}

private static void BufferedReaderReadFile(String filepath)

{

try

{

StringBuffer sb = new StringBuffer();

BufferedReader br = new BufferedReader(new FileReader(new File(filepath)));

String readLine = "";

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

{

sb.append(readLine + "\n");

}

br.close();

System.out.print(sb.toString());

}

catch (FileNotFoundException e)

{

e.printStackTrace();

}

catch (IOException e)

{

e.printStackTrace();

}

}

private static void FileInputStreamReadFile(String filepath)

{

try

{

File file = new File(filepath);

FileInputStream fis = new FileInputStream(file);

long filelength = file.length();

byte[] bb = new byte[(int)filelength];

fis.read(bb);

fis.close();

System.out.println(new String(bb));

}

catch (FileNotFoundException e)

{

e.printStackTrace();

}

catch (IOException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

不知道你的文件格式,不过你可以可以尝试用io流来读取。下面代码 我试过是可以读取挺多格式文件的,你试下。

java字节流怎么读取数据

字节流读取数据例子如下:

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

/**

* 复制文件

* @author young

*

*/

public class CopyFile {

public static void main(String[] args) {

/* 指定源exe文件的存放路径 */

String str = "f:/jdk-1_5_0_06-windows-i586-p.exe";

/* 指定复制后的exe的目标路径 */

String strs = "e:/copy.exe";

/* 创建输入和输出流 */

FileInputStream fis = null;

FileOutputStream fos = null;

try {

/* 将io流和文件关联 */

fis = new FileInputStream(str);

fos = new FileOutputStream(strs);

byte[] buf = new byte[1024 * 1024];

int len;

while ((len = fis.read(buf)) != -1) {

fos.write(buf, 0, len);

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

fis.close();

fos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

java system.in.read()如何读取数据

system.in.read()做不到,

用输入输出流吧:

很长很多的代码,我学一年才记住的,现在忘了……

查了查:先加这个包:import

java.io.*;()主函数代码如下:

public

static

void

main(String[]

args)

throws

IOException{

InputStreamReader

reader

=

new

InputStreamReader(System.in);

BufferedReader

input

=

new

BufferedReader(reader);

String

s

=

input.readLine();/*执行输入流操作*/

int

x

=

Integer.parseInt(s);/*加个int型的转换*/

跪求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读取数据流代码 java 读取流
链接地址:http://bjjierui.cn/article/dddiohs.html

其他资讯