我有一个整数,字符串(K,V)的hashMap,并且只想将字符串值写入文件(而不是键整数),我只想写一些第一个n条目(没有特定的顺序)到文件而不是整个地图.我已经尝试过四处寻找,但是找不到写入第一个n条目的方法.(有些例子我可以将值转换为字符串数组然后再进行操作]但是它没有提供正确的格式我要写的文件)
我正在学习 Java 并从事文件 IO 工作,现在一直停留在从一个文件读取文本并写入另一个文件中。我使用两种不同的方法,第一种方法用于在控制台中从文件#1 读取和显示文本,另一种方法用于在文件#2 中写入。
我可以成功读取并显示 File#1 中的内容,但不确定如何在 file#2 中写入文本。
这是我到目前为止编写的代码:
import java.io.*;
public class ReadnWrite {
public static void readFile() throws IOException {
BufferedReader inputStream = new BufferedReader(new FileReader(
"original.txt"));
String count;
while ((count = inputStream.readLine()) != null) {
System.out.println(count);
}
inputStream.close();
}
public static void writeFile() throws IOException{
BufferedWriter outputStream = new BufferedWriter(new FileWriter(
"numbers.txt"));
//Not sure what comes here
}
public static void main(String[] args) throws IOException {
checkFileExists();
readFile();
}
}
Run Code Online (Sandbox Code Playgroud)
这只是为了我自己的学习,因为有很多例子可以阅读和编写,而无需使用不同的方法,但我想了解如何通过不同的方法来实现。
任何帮助将不胜感激。
问候,
首先,我想提一下,我在java中并没有真正的经验,我搜索了StackOverFlow来解决我的问题,要么我没有找到它或者没有理解答案,所以我现在要问:
我想开始使用BufferedReader,并没有找到任何我理解的指南,所以我从这里和那里拿到了一些并写了这个例子:
BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
int x = Integer.parseInt(input.readLine());
String y = input.readLine();
System.out.println(x);
Run Code Online (Sandbox Code Playgroud)
这个代码工作的输入34,然后再进入abc,但在什么即时试图达到我需要的输入34 abc通过空间分隔开,以inputed在一起,并且x将得到34和y将得到abc.这在使用扫描仪时会起作用,但问题是扫描仪超时我正在做的练习因为它很慢.
是否有任何简单的方法可以将这些输入空间分开,就像使用Scanner一样?
我使用缓冲读取器读取充满信息行的文件.一些较长的文本行扩展为多行,因此缓冲区将其视为新行.每行以';'符号结尾.所以我想知道是否有办法使缓冲读取器读取一行直到它到达';'然后返回整行作为字符串.这是我到目前为止如何使用缓冲读卡器.
String currentLine;
while((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
String[] line = currentLine.split(" ");
String fir = line[1];
String las = line[2];
for(int c = 0; c < players.size(); c++){
if(players.get(c).getFirst().equals(fir) && players.get(c).getLast().equals(las) ){
System.out.println(fir + " " + las);
String text2 = currentLine.replaceAll("[.*?]", ".150");
writer.write(text2 + System.getProperty("line.separator"));
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试检查文件内容是否为空。我有一个内容为空的源文件。我尝试了不同的选择。但没有什么对我有用。
这是我的代码:
Path in = new Path(source);
/*
* Check if source is empty
*/
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(fs.open(in)));
} catch (IOException e) {
e.printStackTrace();
}
try {
if (br.readLine().length() == 0) {
/*
* Empty file
*/
System.out.println("In empty");
System.exit(0);
}
else{
System.out.println("not empty");
}
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我试过使用 -
1. br.readLine().length() == 0
2. br.readLine() == null
3. br.readLine().isEmpty()
Run Code Online (Sandbox Code Playgroud)
以上所有内容都不是空的。我需要使用 -
BufferedReader br = null;
try { …Run Code Online (Sandbox Code Playgroud) 我试图比较两个随机化的文本文件,并打印出两个文件中匹配的行.档案1:
Student1
Student2
Student3
Student4
Run Code Online (Sandbox Code Playgroud)
文件2:
Student6
Student1
Student2
Run Code Online (Sandbox Code Playgroud)
我希望输出为
Student1
Student2
Run Code Online (Sandbox Code Playgroud)
我的代码如下.
public static void main(String[] args) throws IOException {
String first = "file1.txt";
String second = "file2.txt";
BufferedReader fBr = new BufferedReader(new FileReader(first));
BufferedReader sBr = new BufferedReader(new FileReader(second));
PrintWriter writer = new PrintWriter("test.txt", "UTF-8");
while ((first = fBr.readLine()) != null) {
String partOne1 = fBr.readLine();
String partTwo1 = sBr.readLine();
while ((second = sBr.readLine()) != null) {
System.out.println(first);
writer.println(first);
break;
}
}
writer.close();
fBr.close();
sBr.close();
Run Code Online (Sandbox Code Playgroud) 由于在用户输入值的情况下,缓冲阅读器比扫描仪类快得多,但是正如在大多数算法竞赛或采访中所观察到的那样,单个输入行上通常有多个整数。因此,使用 Scanner 类变得更容易 -
Scanner in=new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
Run Code Online (Sandbox Code Playgroud)
如果是 Buffered Reader,您必须先输入一行(因为没有 readInt 选项),然后根据其上的整数数解析该行 -
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a,b;
String line = br.readLine();
String[] strs = line.trim().split("\\s+");
a=Integer.parseInt(strs[0]);
b=Integer.parseInt(strs[1]);
Run Code Online (Sandbox Code Playgroud)
虽然在后一种情况下输入可能会更快,但是解析将获得的字符串分成单个整数会不会花费很多时间?因此,在这种情况下,以上哪一个更优化或更快?
谢谢你。
我试图学习如何使用Java中的FileReader来读取文件但是我遇到了持久性错误.我正在使用Eclipse,我得到一个红色错误,表明构造函数FileReader(File)未定义,构造函数BufferedReader(FileReader)未定义; 但是,我不知道这个错误源自何处,因为我正在使用正确的库和语句.
我收到以下错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor FileReader(File) is undefined
The constructor BufferedReader(FileReader) is undefined
at FileReader.main(FileReader.java:17)
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReader {
public static void main(String[] args) {
File file = new File("example.txt");
BufferedReader br = null;
try {
FileReader fr = new FileReader(file);
br = new BufferedReader(fr);
String line;
while( (line = br.readLine()) != null ) {
System.out.println(line);
}
} catch (FileNotFoundException e) …Run Code Online (Sandbox Code Playgroud) 所以我正在阅读一个包含我在我的代码中早先写过的约会的文件.我想筛选文本文件并在某个日期查找约会并将它们添加到ArrayList中,但是当BufferedReader通过它时,它会跳过其他行...继承我的代码
public ArrayList<String> read(int checkDay, int checkMonth, int checkYear) {
ArrayList<String> events = new ArrayList<String>();
BufferedReader in = null;
String read;
try {
in = new BufferedReader(new FileReader("calendar.txt"));
while ((read = in.readLine()) != null) {
read = in.readLine();
String[] split = read.split(",");
System.out.println(read);
if (split[1].equals(Integer.toString(checkDay)) && split[2].equals(Integer.toString(checkMonth)) && split[3].equals(Integer.toString(checkYear))) {
events.add(split[0] + " : " + split[1] + "/" + split[2] + "/" + split[3]);
}
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) 我们需要为netbeans的本地电影院完成一个简单的票务系统,而我为两个问题感到困惑。
问题1作为输出的一部分,一旦您选择了票证类型+数量,就需要有一个输出“您正在购买Y数量的X张票证”
问题2是长者票需要花费$ 32.50,我似乎无法找到一种允许使用小数进行计算的解决方法。我进行了调试,似乎将数字更改为整数,然后该整数将无法正确计算。救命!
package ticketingsystem;
import java.io.*;
public class ticketingsystem
{
public static void main(String []args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String order,again;
int quantity,price1=0,price2=0, price3=0,loop1=0,quantity1,quantity2=0;
System.out.println(" ");
System.out.println("Welcome to the cinemas!");
System.out.println(" ");
System.out.println("MAIN MENU");
System.out.println(" ");
System.out.println("The cinema has the following options");
System.out.println(" ");
System.out.println("1 = Child (4-5 yrs)");
System.out.println("2 = Adult (18+ yrs)");
System.out.println("3 = Senior (60+ yrs)");
do{
System.out.println(" ");
System.out.print("Enter your option: ");
order=br.readLine();
if (order.equalsIgnoreCase("1")) {
price1=18;
} else if (order.equalsIgnoreCase("2")) …Run Code Online (Sandbox Code Playgroud) bufferedreader ×10
java ×10
file ×3
file-io ×1
filewriter ×1
hadoop ×1
hashmap ×1
input ×1
lines ×1
mapreduce ×1
printwriter ×1
readline ×1
while-loop ×1