BufferedReader bre = null
try {
String file = "D:/test/test.txt"
bre = new BufferedReader(new FileReader(file))//此时获取到的bre就是整个文件的缓存流
while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环
{
System.out.println(str)//原样输出读到的内容
};
备注: 上面的bre就是提问者需要的流。流用完之后必须close掉,如上面的就应该是:bre.close(),否则bre流会一直存在,直到程序运行结束。
在源文件头加入#include
<fstream>
要将数据从内存写到文件:
ofstream
output("***.txt")
这样将文件与输出文件流(output)关联起来
然后output<<a<<"
"<<b<<"
"<<c<<endl即可
从文件读到内存则
ifstream
input("***.txt")
其他类似
注意文件名必须为c字符串
若为string类型
则需要将之转换为c串:
string
str
cin>>str
ifstream
input(str.data())//或者ifstream
input(str.c_str())
using namespace Systemusing namespace System::IO
using namespace System::Text
int main()
{
String^ path = "c:\\temp\\MyTest.txt"//文件的路径
{
FileStream^ fs = File::Create( path )//创建文件
try
{
AddText( fs, "This is some text" )
AddText( fs, "This is some more text," )
AddText( fs, "\r\nand this is on a new line" )
AddText( fs, "\r\n\r\nThe following is a subset of characters:\r\n" )
for ( int i = 1i <120i++ )
{
AddText( fs, Convert::ToChar( i ).ToString() )
if ( Math::IEEERemainder( Convert::ToDouble( i ), 10 ) == 0 )
{
AddText( fs, "\r\n" )
}
}
}
finally
{
if ( fs )
delete (IDisposable^)fs
}
}
{
FileStream^ fs = File::OpenRead( path )//打开文件,后面代码是完成读文件内容
try
{
array<Byte>^b = gcnew array<Byte>(1024)
UTF8Encoding^ temp = gcnew UTF8Encoding( true )
while ( fs->Read( b, 0, b->Length ) >0 )
{
Console::WriteLine( temp->GetString( b ) )
}
}
finally
{
if ( fs )
delete (IDisposable^)fs
}
}
}
void AddText( FileStream^ fs, String^ value ) //写入文件
{
array<Byte>^info = (gcnew UTF8Encoding( true ))->GetBytes( value )
fs->Write( info, 0, info->Length )
}
这够全了吧 =。=
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)