将部分行写入文件 [英] Writing part of a line to a file

查看:85
本文介绍了将部分行写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RandomAccessFile readName = null;

RandomAccessFile nameWriter = null;

List< String> Names = new ArrayList< String>();

char [] pan = new char [19];

char [] cardName = new char [23];

尝试{

readName = new RandomAccessFile(persoNames +" /perso.txt" ;,rw);

nameWriter = new RandomAccessFile( " C:/perso.txt"," rw");

for(String line = readName.readLine(); line!= null;

line = readName.readLine()){

if(line!= null){

// readName。

line.getChars(0,19 ,pan,0);

line.getChars(19,42,cardName,0);

nameWriter.writeChars(pan.toString());


我试图使用上面的代码将字符数组中的信息写入文件。我第一次得到了正确的东西然后我得到了垃圾。


你能指引我到任何网页资源来帮助我纠正代码。

RandomAccessFile readName = null;
RandomAccessFile nameWriter= null;
List<String> Names = new ArrayList<String>();
char[] pan= new char[19];
char[] cardName= new char[23];
try{
readName= new RandomAccessFile(persoNames + "/perso.txt","rw");
nameWriter= new RandomAccessFile("C:/perso.txt","rw");
for (String line = readName.readLine(); line != null;
line = readName.readLine()) {
if (line != null){
//readName.
line.getChars(0,19,pan,0);
line.getChars(19,42,cardName,0);
nameWriter.writeChars(pan.toString());

I tried to use the above code to write the info in the character array to a file. I get the correct thing first time then later I get garbage.

Could you direct me to any web resources to help me correct the code.

推荐答案


RandomAccessFile readName = null;

RandomAccessFile nameWriter = null;

List< String> Names = new ArrayList< String>();

char [] pan = new char [19];

char [] cardName = new char [23];

尝试{

readName = new RandomAccessFile(persoNames +" /perso.txt" ;,rw);

nameWriter = new RandomAccessFile( " C:/perso.txt"," rw");

for(String line = readName.readLine(); line!= null;

line = readName.readLine()){

if(line!= null){

// readName。

line.getChars(0,19 ,pan,0);

line.getChars(19,42,cardName,0);

nameWriter.writeChars(pan.toString());


我试图使用上面的代码将字符数组中的信息写入文件。我第一次得到了正确的东西然后我得到了垃圾。


您能指导我使用任何网络资源来帮助我纠正代码。
RandomAccessFile readName = null;
RandomAccessFile nameWriter= null;
List<String> Names = new ArrayList<String>();
char[] pan= new char[19];
char[] cardName= new char[23];
try{
readName= new RandomAccessFile(persoNames + "/perso.txt","rw");
nameWriter= new RandomAccessFile("C:/perso.txt","rw");
for (String line = readName.readLine(); line != null;
line = readName.readLine()) {
if (line != null){
//readName.
line.getChars(0,19,pan,0);
line.getChars(19,42,cardName,0);
nameWriter.writeChars(pan.toString());

I tried to use the above code to write the info in the character array to a file. I get the correct thing first time then later I get garbage.

Could you direct me to any web resources to help me correct the code.



您想在文件中的特定位置插入文本还是将其追加到末尾?

为什么使用两个RandomAccessFile对象? ?

You want to insert text at a particular position in a file or append it to the end?
Why are you using two RandomAccessFile objects?



您想在文件中的特定位置插入文本还是将其追加到最后?

为什么使用两个RandomAccessFile对象?
You want to insert text at a particular position in a file or append it to the end?
Why are you using two RandomAccessFile objects?



/ *

* NamesCount.java

*

*创建于2007年9月13日,14:06

*

*要更改此模板,请选择工具|模板管理员

*并在编辑器中打开模板。

* /


包名称数量;

import java.io. *;

import java.util。*;

/ **

*

* @author oshonowo

* /

公共类NamesCount {

public static void main(String [] args)抛出异常{

文件目录=新文件(" // 10.10.0.127/c


/*
* NamesCount.java
*
* Created on 13 September 2007, 14:06
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package namescount;
import java.io.*;
import java.util.*;
/**
*
* @author oshonowo
*/
public class NamesCount {
public static void main(String[] args) throws Exception{
File Dir = new File("//10.10.0.127/c


/ Documents and Settings / oshonowo / My Documents / Chams to be burn / 20070921" );

List< String> allNames = new ArrayList< String>();

if(Dir.exists()&& Dir.isDirectory()){

String [] location = Dir .list();

for(int i = 0; i< location.length; i ++){

File dirs = new File(Dir +" /") ; + location [i]);

// SumOfCards + = NumberofLines(dirs);

allNames.add(PrintNames(dirs));

}

}

System.out.println(" Names is" + allNames);

//System.out.println (总和卡数是+ + SumOfCards);


}


静态字符串PrintNames(File persoNames)抛出异常{

RandomAccessFile readName = null;

RandomAccessFile nameWriter = null;

List< String> Names = new ArrayList< String>();

char [] pan = new char [19];

char [] cardName = new char [23];

尝试{

readName = new RandomAccessFile(persoNames +" /perso.txt" ;,rw);

nameWriter = new RandomAccessFile( " C:/perso.txt"," rw");

for(String line = readName.readLine(); line!= null;

line = readName.readLine()){

if(line!= null){

// readName。

line.getChars(0,19 ,pan,0);

line.getChars(19,42,cardName,0);

nameWriter.writeChars(pan.toString());

// for(int i = 0; i< pan.length; i ++){nameWriter.writeChars(pan。);}

// for(int i = 0; i< cardName.length; i ++){nameWriter.writeBytes(cardName [i]);}

//System.out.println(pan +" " + cardName);

Names.add(line);

nameWriter.writeBytes(System.getProperty(" line.sep arator"));

}

}

//Names.add("%n");

if(Names.isEmpty()!= true){

Collections.sort(Names);

//System.out.println(Names);

nameWriter.writeBytes(Names.toString( ));

}

} catch(例外e){

System.err.format(&无法读取%s:% s%n",Names,e);

System.exit(1);

} finally {

if(readName!= null) ){

尝试{

readName.close();

} catch(IOException e){}

}

if(nameWriter!= null){

试试{

nameWriter.close();

} catch(IOException e){}

}

}

返回Names.toString();


}


/ * static int NumberofLines(文件文件)抛出IOException {

BufferedReader reader = null;

int lineCount = 0;

try {

reader = new BufferedReader( new FileReader(file +" /perso.txt"));

for(String line = reader.readLine(); line!= null;

line = reader.readLine()){

lineCount ++;

}

} catch(IOException e){

System.err.format(无法读取%s:%s%n,文件,e);

System.exit (1);

}最后{

if(reader!= null){

try {

reader .close();

} catch(IOException e){}

}

}

System.out .println("行数是" + lineCount);

返回lineCount;

} * /

}


我正在尝试编写一个程序,它将读取另一个系统上目录的内容并将相关部分打印到文件中。


我只是需要知道在哪里可以找到有关如何读取表示信息行的字符串部分的信息相关部分并将它们写入不同的文件。


我尝试在sun网站上使用API​​,但对于像我这样的初学者来说这有点困难。


这是办公室工作。请帮忙。
/Documents and Settings/oshonowo/My Documents/Chams to be burnt/20070921");
List<String> allNames = new ArrayList<String>();
if( Dir.exists() && Dir.isDirectory()){
String[] location = Dir.list();
for(int i=0; i <location.length; i++){
File dirs= new File(Dir + "/" + location[i]);
//SumOfCards+=NumberofLines(dirs);
allNames.add(PrintNames(dirs));
}
}
System.out.println(" Names are "+ allNames);
//System.out.println("Sum of cards is "+ SumOfCards);

}

static String PrintNames(File persoNames) throws Exception{
RandomAccessFile readName = null;
RandomAccessFile nameWriter= null;
List<String> Names = new ArrayList<String>();
char[] pan= new char[19];
char[] cardName= new char[23];
try{
readName= new RandomAccessFile(persoNames + "/perso.txt","rw");
nameWriter= new RandomAccessFile("C:/perso.txt","rw");
for (String line = readName.readLine(); line != null;
line = readName.readLine()) {
if (line != null){
//readName.
line.getChars(0,19,pan,0);
line.getChars(19,42,cardName,0);
nameWriter.writeChars(pan.toString());
//for (int i=0; i< pan.length; i++){ nameWriter.writeChars(pan.);}
//for (int i=0; i< cardName.length; i++){nameWriter.writeBytes(cardName[i]);}
//System.out.println(pan + " " + cardName);
Names.add(line);
nameWriter.writeBytes(System.getProperty("line.sep arator"));
}
}
//Names.add("%n");
if (Names.isEmpty()!= true){
Collections.sort(Names);
//System.out.println(Names);
nameWriter.writeBytes(Names.toString());
}
}catch (Exception e) {
System.err.format("Could not read %s: %s%n", Names, e);
System.exit(1);
} finally {
if (readName != null) {
try {
readName.close();
} catch (IOException e) {}
}
if (nameWriter != null) {
try {
nameWriter.close();
} catch (IOException e) {}
}
}
return Names.toString();

}

/* static int NumberofLines(File file) throws IOException{
BufferedReader reader = null;
int lineCount = 0;
try {
reader = new BufferedReader(new FileReader(file + "/perso.txt"));
for (String line = reader.readLine(); line != null;
line = reader.readLine()) {
lineCount++;
}
} catch (IOException e) {
System.err.format("Could not read %s: %s%n", file, e);
System.exit(1);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {}
}
}
System.out.println("Number of Lines are " + lineCount);
return lineCount;
}*/
}

I am trying to write a program that will read the contents of a directory on another system and prints the relevant parts to a file.

I just need to know where I can find info on how to read part of a string representing a line of info., extract the relevant parts and write them to a different file.

I have tried using the API on the sun website but it is a bit hard for a beginner like me.

This is office work. Pls help.


这篇关于将部分行写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆