我和我的伙伴正在为大学的面向对象编程课程开发一个程序.我们正在尝试将文本作为信息数据库写入文件.问题是当我们尝试用BufferedReader读取相应的行时,我们似乎无法弄清楚如何读取正确的行.唯一可用的函数似乎是read(),它只读取一个字符.readLine()只读取一行(不是我们想要它读取的行.skip()只跳过指定的一些字符.任何人都知道如何告诉程序我们想要读取哪一行?我们的方法getAnswer()使用参数int rowNumber是我们要做的:超类:http://pastebin.com/d2d9ac07f 子类是无关紧要的(主要是因为我们还没有编写它).当然它是我们正在使用的Java.先谢谢.
我之前试过问过这个问题,但我的问题并不清楚.Java BufferedReader对字符的操作?
这是我的问题..我有一个BufferedReader设置为从设备读取.它阅读得很好.我把它设置为
if (Status.reader.ready()) {
Lines = Status.reader.readLine();
}
if (Lines.contains(">")) {
log.level1("ready to send data")
}
Run Code Online (Sandbox Code Playgroud)
在>我向设备发送更多数据之前,缓冲读卡器不会报告.问题是,当读者包含>它时,它没有报告准备就绪.它保持>直到我输入更多数据.
我尝试了以下内容,它什么也没有返回.它甚至没有返回log.level0()
Lines = ""
try {
Lines = Status.reader.readLine();
} catch (IOException e) {
Log.level0("Attempted to read blank line");
}
Run Code Online (Sandbox Code Playgroud)
这是发送的实际数据:
^M^M01 02 F3^M00 01 F3 3E^M>
Run Code Online (Sandbox Code Playgroud)
但是BufferedReader会忽略>直到发送更多数据,然后得到如下结果:
>0102
Run Code Online (Sandbox Code Playgroud)
当我从命令提示符检查设备的实际数据时,它返回我所期望的,>它存在.
BufferedReader不会给我的>.有什么方法我可以检查这个char否则?
我正在尝试读取文本文件,我正在使用fileImputStream,并将所有行读入单个String,然后将其输出到控制台(System.out)
当我尝试阅读humanSerf.txt时,它在consol中给了我这个:
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\paperw11900\paperh16840\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural\pardirnatural
\f0\fs24 \cf0 symbol=HS\
strength=15\
agility=13\
constitution=7\
wisdom=9\
intelligence=5}
Run Code Online (Sandbox Code Playgroud)
在文本文件中,它说:
symbol=HS
strength=15
agility=13
constitution=7
wisdom=9
intelligence=5
Run Code Online (Sandbox Code Playgroud)
如何让奇怪的文字消失?
这是我正在使用的代码,请帮忙
try{
// Open the file that is the first
// command line parameter
FileInputStream read = new FileInputStream("resources/monsters/human/humanSerf.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(read);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the …Run Code Online (Sandbox Code Playgroud) 我正在尝试用字符串写一个文件,然后阅读它.但是,当我看到它看起来很奇怪与正方形和东西..我无法复制粘贴!
这是代码:
import java.io.*;
public class ObjectOutputStreamDemo {
public static void main(String[] args) throws ClassNotFoundException {
try {
//Now Im writing
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("fruit.dat"));
String ord[] = { "Banana", "Mango", "Apple", "Passionfruit","Orange" };
for (int i = 0; i < 5; i++) {
output.writeObject(ord[i]);
}
output.close();
//Now Im trying to read.
BufferedReader in = new BufferedReader(new FileReader("fruit.dat"));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
} catch (IOException e) {
System.out.println("Problem with file.");
} …Run Code Online (Sandbox Code Playgroud) 我有以下问题:我需要输入一个12行的文件.每行包含8个字符.我必须在一个包含8行和12个字符的文件中输出它.我必须逐行读取输入并同时输出每一行.所以我不允许先读取我的输入,在我读完之后只需要输入8行,每行12个字符.我正在使用BufferedReader来读取我的文件,使用BufferedWriter来写入我的文件.所以通过例子:
输入:
12345678
qwertyui
asdfghjk
产量:
12345678qwer
tyuiasdfghjk
编辑:这确实是一项家庭作业.
BufferedWriter bufferedWriter = null;
FileReader fr;
try {
fr = new FileReader(new File(directory to file));
bufferedWriter = new BufferedWriter(new FileWriter(directory to file);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
bufferedWriter.write(output);
bufferedWriter.newLine();
line = br.readLine();
}
br.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
//Close the BufferedWriter
try {
if (bufferedWriter != null) {
bufferedWriter.flush();
bufferedWriter.close();
}
} catch (IOException ex) {
ex.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) 我正在开发一个在Android和iOS设备上运行的应用程序.对于这个应用程序,我需要从URL获取XML流.这个XML并不是很安全,因为有些行,例如:
启示
会变成 :
Rvlation
当然,我知道最好的办法是修复XML生成器脚本.但我只是作为一家公司的开发人员而无法访问它,所以目前我正在努力做我能做的事情.
现在这是本主题的原因.当我将这些数据放在Objective-C的NSData对象中时:
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
Run Code Online (Sandbox Code Playgroud)
然后尝试读取每个字节:
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);
for(int i = 0 ; i < len ; i++)
{
NSLog(@"%d",byteData[i]);
}
Run Code Online (Sandbox Code Playgroud)
它正确显示char的int值,是否为特殊字符.然后我只需要(unichar)byteData[i]解决它.
没有Java和Android,我正在尝试做一个基本的BufferedReader操作.
URL myURL = new URL(url);
BufferedReader in = new BufferedReader(new InputStreamReader(myURL.openStream()));
Run Code Online (Sandbox Code Playgroud)
然后逐个打印每个char的int:
int i;
while((i = in.read()) != -1) System.out.print(i);
Run Code Online (Sandbox Code Playgroud)
但是使用Java,通过这样做,我立即得到替换char的id(65533)而不是好的id,并且无法设法替换它.
任何的想法?谢谢你读我
我正在尝试逐行读取文件,但每次运行程序时,我都会在行中得到一个NullPointerException,spaceIndex = line.indexOf(" ");这显然意味着该行为null.然而.我知道我正在使用的文件正好有7行(即使我打印了值numLines,我得到了值7.然而当我尝试读取一行到我的字符串时,我仍然得到一个nullpointerexception.
// File file = some File I take in after clicking a JButton
Charset charset = Charset.forName("US-ASCII");
try (BufferedReader reader = Files.newBufferedReader(file.toPath(), charset)) {
String line = "";
int spaceIndex;
int numLines = 0;
while(reader.readLine()!=null) numLines++;
for(int i = 0; i<numLines; i++) {
line = reader.readLine();
spaceIndex = line.indexOf(" ");
System.out.println(spaceIndex);
}
Run Code Online (Sandbox Code Playgroud)
PS :(我实际上并没有使用这段代码来打印空间的索引,我替换了循环中的代码,因为它有很多,而且会让它读起来更长)
如果我要以错误的方式阅读这些行,那么如果有人可以提出另一种方式会很好,因为到目前为止,我尝试过的每一种方式都给了我同样的例外.谢谢.
在使用BufferedReader读取文件时,我希望它跳过以"#"开头的空白行和行.最终,每个角色都被添加到一个arraylist
inputStream = new BufferedReader(new FileReader(filename));
int c = 0;
while((c = inputStream.read()) != -1) {
char face = (char) c;
if (face == '#') {
//skip line (continue reading at first char of next line)
}
else {
faceList.add(face);
}
Run Code Online (Sandbox Code Playgroud)
除非我弄错了,否则BufferedReader会自动跳过空白行.除此之外,我该怎么做呢?
我会跳过()吗?线条的长度可能会有所不同,所以我认为这不会起作用.
在类下面运行下面的代码 FlightSearch
String moreSearch = "y";
List<Flight> resultList;
// load initial flight data into DB
if (!init()) {
return;
}
// A background thread to monitor changes in csv repository
FileListner fl = new FileListner();
fl.start();
do {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
// main thread gets input
Input inputQuery = new Input();
try {
inputQuery.getQuery();
} catch (InvalidException e1) {
e1.getMessage();
} finally {
fl.stopThread();
}
// main thread STARTs processing task as background …Run Code Online (Sandbox Code Playgroud) 我似乎无法得到底部"我"链接到下面的for循环中的变量我哪里出错?我试图编辑它更改变量并将变量放在for循环上面所有我得到的是错误
我也在使用eclipse luna
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class AssignmentProgramming {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a string");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String data = reader.readLine();
char ch =(char) System.in.read();
int letters=0;
for(int i=0; i<data.length(); i++);
{
char c=data.charAt(i);//<-- This i here
if (c==ch);
{letters++;
}
}
System.out.println(letters);
}
}
Run Code Online (Sandbox Code Playgroud) bufferedreader ×10
java ×10
file ×2
encoding ×1
ioexception ×1
lines ×1
nsdata ×1
objective-c ×1
path ×1
skip ×1
variables ×1