0

我需要删除文本文件的最后 3 行,或者使用 ActionScript 3.0 重写最后 3 行

我了解如何附加文件,但我不知道如何使用文件流计算行数。

4

2 回答 2

1

尝试这个,

var loader:URLLoader = new URLLoader(); //Creating a loader object
var myArray : Array;
loader.addEventListener(Event.COMPLETE, onLoaded); //On complete event handler

function onLoaded(e:Event):void {
   var myArray:Array = e.target.data.split(/\n/);
   //once you have all the lines as elements of the array you can change them
   myArray[myArray.length -1] = "ABC";
   myArray[myArray.length -2] = "DEF";
   myArray[myArray.length -3] = "GHI";
}

loader.load(new URLRequest("myFile.txt"));

//Then write out the contents of the array,

var f:File=new File("path\to\file.txt");
var handle:FileStream=new FileStream();
handle.open(f, FileMode.WRITE);
handle.writeUTFBytes(myArray);
handle.close();
于 2013-05-30T04:01:06.450 回答
1

或者试试:

private function trimTextFile(txtFile:File, trimNum:uint):Boolean{
    if(txtFile.exists){
        var stream:FileStream = new FileStream();
        stream.open(txtFile, FileMode.READ);
        var tmp:Array = stream.readUTFBytes(stream.bytesAvailable).split("\n");
        stream.close();
                
        stream.open(txtFile, FileMode.WRITE);
        for(var i:int = 0; i < tmp.length - trimNum; i++){
            stream.writeUTFBytes(tmp[i] +"\n");
        }
        stream.close();
        return true;
    }else{
        return false;
    }
            
}

像这样称呼它

this.trimTextFile(File.desktopDirectory.resolvePath("test.txt"), 3);

于 2013-05-30T04:38:14.950 回答