從文件中刪除行
回答
除非是最後一行,否則不能在沒有臨時文件的情況下執行此操作。你可以用覆蓋文本行(雖然這不像你想象的那麼容易,除非行的大小是固定的) - 如果有某種方法可以指示讀取文件的某些行應該被忽略,那麼這是一個選項。但是不能通過從中間刪除數據來減小文件大小。這絕不是大多數文件系統支持的操作。
同樣,您不能在文件中插入額外的信息。基本上可用的操作是覆蓋,附加和截斷。
我確實相信某些操作系統可以截斷文件。不過,它不是標準Java運行時的一部分。 –
@Thorbjørn:這就是爲什麼我在「不是大多數文件系統支持的操作」中包含「最多」的原因。:) –
你絕對可以做到。認爲這將是一個挑戰和System.Text.Encoding
ninjitsu好測試。這裏你去:
static bool DeleteEveryLineAfter(Stream data, string search, Encoding encoding)
{
const int WindowsPageSize = 4096;
var decoder = encoding.GetDecoder();
var encoder = encoding.GetEncoder();
var buffer = new byte[WindowsPageSize];
var length = 0;
var searchPosition = 0;
var newLine = Environment.NewLine.ToCharArray();
var newLinePosition = 0;
var currentLinePosition = 0;
var lineIsValid = true;
while ((length = data.Read(buffer, 0, WindowsPageSize)) != 0)
{
// Get the characters.
var chars = new char[decoder.GetCharCount(buffer, 0, length, false)];
decoder.GetChars(buffer, 0, length, chars, 0, false);
for (var i = 0; i < chars.Length; i++)
{
var c = chars[i];
if (
// Only if the line isn't LONGER than what we are searching for.
searchPosition < search.Length &&
// And if the current character matches.
search[searchPosition] == c &&
// And we have matched something in it, or it is the start of the line.
(searchPosition > 0 || currentLinePosition == 0) &&
// And if a previous character didn't fail.
lineIsValid
)
{
searchPosition++;
newLinePosition = 0;
}
else if (newLinePosition < newLine.Length && c == newLine[newLinePosition])
{
newLinePosition++;
// Did we match the whole newline.
if (newLinePosition == newLine.Length)
{
// Was the current line a match?
if (lineIsValid && searchPosition == search.Length)
{
// Figure out the different between the amount of data
// we read, and how long the line was.
var bytesLeft = encoder.GetByteCount(chars, i, chars.Length - i, true);
var finalLength = data.Position - bytesLeft;
data.SetLength(finalLength);
return true;
}
else
{
lineIsValid = true;
newLinePosition = 0;
searchPosition = 0;
currentLinePosition = -1; // Compensate for ++ below.
}
}
}
else
{
lineIsValid = false;
}
currentLinePosition++;
}
}
return false;
}
哦,它是標記爲Java的。哈哈,還有C#給你。翻譯成Java留給學生看。 –
- 1. 從文件中刪除行
- 2. 從文件中刪除行
- 3. Powershell - 從文本文件中刪除行
- 4. 從文本文件中刪除一行?
- 5. 從文本文件中刪除一行
- 6. 從文本文件中刪除行
- 7. 從文本文件中刪除多行
- 8. 從from.txt文件中刪除文本行
- 9. 從txt文件中刪除前兩行
- 10. 從php文件中刪除特殊行
- 11. 從大文件中刪除一行php
- 12. Qt從文件中刪除一行
- 13. 從更新csv文件中刪除行
- 14. 從txt文件中刪除一行java
- 15. 從文件中刪除空行
- 16. 從文件中刪除空行
- 17. 從CSV文件中刪除行
- 18. 從csv文件中刪除換行
- 19. Java從文件中刪除行
- 20. 從文件中刪除非ASCII *行*
- 21. Python:從文件中刪除某些行
- 22. 從txt文件中刪除空行
- 23. 從文件中刪除第一行
- 24. 從文件中刪除特定的行
- 25. C++從.txt文件中刪除行
- 26. Bash:從fasta文件中刪除行
- 27. VBS從文件中刪除行
- 28. 從XML文件中刪除字符行
- 29. 從表中刪除,並刪除文件
- 30. 從表中刪除行時刪除文件
你爲什麼要刪除行?只有當你的文件是50000以上的行會影響你的速度。閱讀時,它會遍歷每一行。將其讀入一個字符串數組,然後刪除該文件或將其清除。如果需要,請閱讀剩餘的/需要的線路。 – LouwHopley
您的請求是不可能的。拿一張紙,在上面畫一些線。現在想象這是一個磁盤上的「文件」。如果您「刪除」論文的第一行,您希望發生什麼?你想在紙上的同一個地方放置所有其他線嗎?那麼,什麼應該「取代」刪除的行?或者您是否想將其他行移動到工作表的頂部?然後你必須閱讀它們並在那裏寫下它們!正如你不能簡單地讓你的紙張的一部分消失一樣,你也不能刪除文件中的一部分。 –
夥計們,看起來他正在要求截斷「從閱讀該行後」。即使您沒有截斷它(首先找到該行,然後將字節向後複製,然後根據搜索行的長度截斷文件),也是如此。 –