zoukankan      html  css  js  c++  java
  • .NET中的文件IO操作实例

    从TextBox控件中写入到txt文本

    //从testbox中写入到txt文本
            protected void Button5_Click(object sender, EventArgs e)
            
    {
                
    string text =
     txtContent.Text;
                
    if (!string
    .IsNullOrEmpty(text))
                
    {
                    
    //指定文件的完整路径

                    string fileName = Server.MapPath("~/txt/test.txt");
                    
    //判断该文件是否存在

                    if (File.Exists(fileName))
                    
    {
                        
    //如果存在,就先删掉

                        File.Delete(fileName);
                    }

                    
    else
                    
    {
                        
    //创建一个文件操作的流

                        FileStream stream = new FileStream(fileName, FileMode.Create);
                        
    //创建一个写操作流

                        StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
                        
    //进行写操作

                        writer.Write(text);
                        //清空控件中的文字
                        txtContent.Text 
    = string
    .Empty;
                        
    //关闭流,不然出现异常

                        writer.Close();
                        stream.Close();
                    }

                }

                
    else
                
    {
                    Response.Write(
    "<script>alert(\"空的列!\")</script>"
    );
                }

            }

    然后再从生成的test.txt中读取数据,显示到TextBox控件中(方法同理)

    //读取文本到textbox中显示
            protected void Button6_Click(object sender, EventArgs e)
            
    {
                
    string fileName = Server.MapPath("~/txt/test.txt"
    );
                
    if
     (File.Exists(fileName))
                
    {
                    FileStream stream 
    = new
     FileStream(fileName, FileMode.Open);
                    StreamReader reader 
    = new
     StreamReader(stream, Encoding.UTF8);
                    txtContent.Text 
    =
     reader.ReadToEnd();
                    reader.Close();
                    stream.Close();
                }

                
    else
                
    {
                    Response.Write(
    "<script>alert(\"没有test.txt文件!\")</script>"
    );
                }

            }
    0
    0
  • 相关阅读:
    黑马程序员JAVA高级视频_IO输入与输出18天2(FileWriter)
    黑马程序员JavaAPI17天5(集合转成数组)
    黑马程序员JAVA高级视频_IO输入与输出19天4(MyBufferedReader)
    android 取消webview的背景色
    DLNA
    Android JNI的若干问题总结
    gcc 一些应用
    如何基于nand flash启动Linux内核(分享一段实用、简单、类似bootloader功能的代码)
    Android JNI开发高级篇有关Android JNI开发中比较强大和有用的功能就是从JNI层创建、构造Java的类或执行Java层的方法获取属性等操作。 一、类的相关操作 1. jclass FindClass(JNIEnv *env, const char *name);
    ubuntu 配置ndk
  • 原文地址:https://www.cnblogs.com/hsapphire/p/1568268.html
Copyright © 2011-2022 走看看