Java文件操作
我在输入文件a.txt中有一些内容Java文件操作,java,logic,Java,Logic,我在输入文件a.txt中有一些内容 Line 1 : "abcdefghijk001mnopqr hellohello" Line 2 : "qwertyuiop002asdfgh welcometologic" Line 3 : "iamworkingherefromnowhere002yes somethingsomething" Line 4 : "thiswillbesolved001here ithink" 我必须读取a.txt文件并将其写入两个单独的文件。即,具有001的行应写入o
Line 1 : "abcdefghijk001mnopqr hellohello"
Line 2 : "qwertyuiop002asdfgh welcometologic"
Line 3 : "iamworkingherefromnowhere002yes somethingsomething"
Line 4 : "thiswillbesolved001here ithink"
我必须读取a.txt文件并将其写入两个单独的文件。即,具有001的行应写入output1.txt,具有002的行应写入output2.txt
有人能帮我用Java编程中的逻辑吗
谢谢,
Naren这里是一个很好的示例,检查条件可以执行以下操作
while ((line = reader.readLine()) != null) {
//process each line in some way
if(line.contains("001") {
fileWriter1.write(line);
} else if (line.contains("002") ) {
fileWriter2.write(line);
}
}
方法sendToFile001()和sendToFile002()编写参数行,如下所示:
ps001.println( line );
使用PrintStream类型的ps001和ps002,在之前打开(在构造函数中?代码完成
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jfile;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
/**
*
* @author andy
*/
public class JFile {
/**
* @param args the command line arguments
*/
static File master = null,
m1 = null, // file output with "001"
m2 = null; // file output with "002"
static FileWriter fw1,
fw2;
static FileReader fr = null;
static BufferedReader br = null;
public static void main(String[] args) throws FileNotFoundException, IOException
{
String root = System.getProperty("user.dir") + "/src/files/";
master = new File ( root + "master.txt" );
m1 = new File ( root + "m1.txt");
m2 = new File ( root + "m2.txt");
fw1 = new FileWriter(m1, true);
fw2 = new FileWriter(m2, true);
fr = new FileReader (master);
br = new BufferedReader(fr);
String line;
while((line = br.readLine())!=null)
{
if(line.contains("001"))
{
fw1.write(line + "\n");
} else if (line.contains("002"))
{
fw2.write(line + "\n");
}
}
fw1.close();
fw2.close();
br.close();
}
}
Project Netbeans:欢迎来到Stackoverflow。你的问题很好。谷歌搜索并尝试一些东西,如果有任何错误或逻辑错误等,请在这里发布代码以获得解决。如果我这样做,我不会写所有的行,只有最后一个迭代的行会被写。如果一行包含001和002怎么办?(删除ifs之间的else)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jfile;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
/**
*
* @author andy
*/
public class JFile {
/**
* @param args the command line arguments
*/
static File master = null,
m1 = null, // file output with "001"
m2 = null; // file output with "002"
static FileWriter fw1,
fw2;
static FileReader fr = null;
static BufferedReader br = null;
public static void main(String[] args) throws FileNotFoundException, IOException
{
String root = System.getProperty("user.dir") + "/src/files/";
master = new File ( root + "master.txt" );
m1 = new File ( root + "m1.txt");
m2 = new File ( root + "m2.txt");
fw1 = new FileWriter(m1, true);
fw2 = new FileWriter(m2, true);
fr = new FileReader (master);
br = new BufferedReader(fr);
String line;
while((line = br.readLine())!=null)
{
if(line.contains("001"))
{
fw1.write(line + "\n");
} else if (line.contains("002"))
{
fw2.write(line + "\n");
}
}
fw1.close();
fw2.close();
br.close();
}
}