
正文
按行读取java代码 java按行读文件
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
用java按行读取字符串,完成一段后按对象存入map中
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
* @author cs12110 2015年8月20日下午4:11:53 测试通过
*
*/
public class SaveToMap {
/**
* FunName: getFileContent TODO: 根据文件名,获取文件内容
*
* @param fileName
* 文件名
* @return
*/
public static String getFileContent(String fileName) {
StringBuffer stringBuffer = new StringBuffer();
File file = new File(fileName);
try {
if (file.exists()) {// 文件存在,则进行内容读取
InputStream inputStream = new FileInputStream(file);
byte[] b = new byte[1024];
int len = -1;
while (-1 != (len = (inputStream.read(b)))) {// 循环读取文件内容
stringBuffer.append(new String(b, 0, len));
//System.out.println(new String(b, 0, len));
}
inputStream.close();// 关闭文件流,节省资源
}
} catch (Exception e) {// 异常处理
e.printStackTrace();
}
return new String(stringBuffer);
}
/**
* FunName: getContent TODO:根据文件,获取tagstr字符串,存放在map里面
*
* @param fileName
* 文件名
* @param tagStr
* 目标字符串
* @return
*/
public static MapString, String getContent(String fileName, String tagStr) {
MapString, String hashMap = new HashMapString, String();
String allStr = getFileContent(fileName);
int count = 0;
for (int index = 0; allStr != null index allStr.length()-tagStr.length(); index++) {
String temp = allStr.substring(index, index+tagStr.length());
if (temp.equals(tagStr)) {
hashMap.put("" + (count++), tagStr);
index += tagStr.length();
}
}
return hashMap;
}
/**
* FunName: displayMap TODO:遍历Map
*
* @param map
* 目标map
*/
public static void displayMap(MapString, String map) {
for (int index = 0; map != null index map.size(); index++) {
System.out.println("Index: "+index+" TagContent: "+map.get("" + index));
}
}
public static void main(String[] args) {
displayMap(getContent("D://test.txt", "haiy"));
}
}
相关问答
Q1: 用java写代码,从TXT中按行读取数据,遇到指定的字符时,输出后面的第二个数值,怎么写,求大神。。。
package com.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
getString(';');
}
public static void getString(char ch) {
FileInputStream inputStream = null;
try {
StringBuffer buffer = new StringBuffer();
inputStream = new FileInputStream(new File("D:\\test.txt"));
int i = 0;
while (i != -1) {
i = inputStream.read();
if ((char) i != ch) {
buffer.append((char) i);
} else {
System.out.println(buffer.toString());
buffer.setLength(0);
}
}
if (i == -1) {
System.out.println(buffer.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Q2: java如何实现按行读取键盘输入?
使用system.in.read可以读取键盘的输入,但是一般不会这样去操作,可以使用java.util.scanner来配合system.in来进行数据的操作,举例如下:
scanner
in=new
scanner(system.in);
string
readline
=
in.nextline();
//读取键盘输入的一行(以回车换行为结束输入)
Q3: 如何用java按行读取文本文件
File file = new File("文件地址");
Scanner scanner = new Scanner(file);
String lineContent = null;
while(scanner.hasNextLine()){//如果有下一行
lineContent = scanner.nextLine();//读取下一行内容
}
scanner.close();//关闭Scanner
Q4: java 按行读取一个文件,存在字符串数组里,一个元素对应一行,再将这个数组按行输出到一个新的文件里
java 按行读取一个文件,存在字符串数组里,一个元素对应一行,再将这个数组按行输出到一个新的文件里,代码如下:
package foo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
File readFile=new File("D://home/a.txt");
InputStream in=null;
InputStreamReader ir=null;
BufferedReader br=null;
OutputStream out=null;
OutputStreamWriter ow=null;
BufferedWriter bw=null;
try {
//用流读取文件
in=new BufferedInputStream(new FileInputStream(readFile));
//如果你文件已utf-8编码的就按这个编码来读取,不然又中文会读取到乱码
ir=new InputStreamReader(in,"utf-8");
br= new BufferedReader(ir);
String line="";
//定义集合一行一行存放
ListString list=new ArrayListString();
//一行一行读取
while((line=br.readLine())!=null){
System.out.println(line);
list.add(line);
}
//将集合转换成数组
String[] arr=list.toArray(new String[list.size()]);
//写入新文件
File newFile=new File("D://home/b.txt");
if(!newFile.exists()){
newFile.createNewFile();
}
out=new BufferedOutputStream(new FileOutputStream(newFile));
//这里也可以给定编码写入新文件
ow=new OutputStreamWriter(out,"gb2312");
bw=new BufferedWriter(ow);
//遍历数组吧字符串写入新文件中
for(int x=0;xarr.length;x++){
bw.write(arr[x]);
if(x!=arr.length-1){
//换行
bw.newLine();
}
}
//刷新该流的缓冲,这样才真正写入完整到新文件中
bw.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
//一定要关闭流,倒序关闭
try {
if(bw!=null){
bw.close();
}
if(ow!=null){
ow.close();
}
if(out!=null){
out.close();
}
if(br!=null){
br.close();
}
if(ir!=null){
ir.close();
}
if(in!=null){
in.close();
}
} catch (Exception e2) {
}
}
}
}
Q5: JAVA编程:读文件,按行输出文件内容
其实你贴的代码并没有问题
不过你可能也发现了,出现了乱码。当然这个乱码不是必然产生的。
这段代码或使用当前环境默认的编码方式去读取test.txt的字符串,如果默认编码与test.txt的编码不一致就可能会导致乱码。
这里附上另一段代码,自定义编码方式
public static void main(String[] args) {
try {
// 将D:/test.txt文件读取到输入流中
InputStream input = new FileInputStream("D:/test.txt");
// 创建BufferedReader,以gb2312的编码方式读取文件
BufferedReader reader = new BufferedReader(new InputStreamReader(input, "gb2312"));
String line = null;
// 按行读取文本,直到末尾(一般都这么写)
while ((line = reader.readLine()) != null) {
// 打印当前行字符串
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
按行读取java代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java按行读文件、按行读取java代码的信息别忘了在本站进行查找喔。







