GVKun编程网logo

使用Writer将int写入文本文件(用write方法写入字符串到指定的文件里)

10

本文将分享使用Writer将int写入文本文件的详细内容,并且还将对用write方法写入字符串到指定的文件里进行详尽解释,此外,我们还将为大家带来关于.net–是否有TextWriter子类在写入文本

本文将分享使用Writer将int写入文本文件的详细内容,并且还将对用write方法写入字符串到指定的文件里进行详尽解释,此外,我们还将为大家带来关于.net – 是否有TextWriter子类在写入文本时触发事件?、android-如何使用kivy从TextInput写入并保存到文本文件、asp.net写入日志到文本文件、BufferedWriter 不会将 Map 的内容写入文本文件;我的世界服务器的相关知识,希望对你有所帮助。

本文目录一览:

使用Writer将int写入文本文件(用write方法写入字符串到指定的文件里)

使用Writer将int写入文本文件(用write方法写入字符串到指定的文件里)

Writer wr = new FileWriter("123.txt");wr.write(123);wr.close();

输出文件包含:
{

问题出在哪里?如何使用写入int文本文件Writer

答案1

小编典典

你必须写String …

你可以试试。

wr.write("123");

要么

wr.write(new Integer(123).toString());

要么

wr.write( String.valueOf(123) );

.net – 是否有TextWriter子类在写入文本时触发事件?

.net – 是否有TextWriter子类在写入文本时触发事件?

我编写了一个接受TextWriter作为参数的方法(通常是Console.Out,但不一定).

当我调用此方法时,会向TextWriter写入一些进度信息.
但是,由于此方法可能会运行很长时间,我想用一些状态信息更新我的UI.

目前,我使用的是StringWriter,但它没有任何事件.所以我第一次从StringWriter获得结果是在方法完成之后.

现在我正在搜索一个继承自TextWriter并触发TextChanged事件或类似事件的类.
我知道这不应该是难以实现的,但我敢打赌CLR团队已经为我做了,我找不到合适的课程.

解决方法

如果有人感兴趣,这是一个扩展StringWriter()类的类,在每次调用writer.Flush()之后触发事件.

我还添加了posibillity以在每次写入后自动调用Flush(),因为在我的情况下,第三方组件,写入控制台,没有执行刷新.

样品用法:

void DoIt()
{
    var writer = new StringWriterExt(true); // true = AutoFlush
    writer.Flushed += new StringWriterExt.FlushedEventHandler(writer_Flushed);

    TextWriter stdout = Console.Out;
    try
    {
        Console.Setout(writer);
        CallLongRunningMethodThatDumpsInfoOnConsole();
    }
    finally
    {
        Console.Setout(stdout);
    }
}

现在我可以及时显示一些状态信息,而不需要等待方法完成.

void writer_Flushed(object sender,EventArgs args)
{
    UpdateUi(sender.ToString());
}

这是班级:

public class StringWriterExt : StringWriter
{
    [Editorbrowsable(EditorbrowsableState.Never)]
    public delegate void FlushedEventHandler(object sender,EventArgs args);
    public event FlushedEventHandler Flushed;
    public virtual bool AutoFlush { get; set; }

    public StringWriterExt()
        : base() { }

    public StringWriterExt(bool autoFlush)
        : base() { this.AutoFlush = autoFlush; }

    protected void OnFlush()
    {
        var eh = Flushed;
        if (eh != null)
            eh(this,EventArgs.Empty);
    }

    public override void Flush()
    {
        base.Flush();
        OnFlush();
    }

    public override void Write(char value)
    {
        base.Write(value);
        if (AutoFlush) Flush();
    }

    public override void Write(string value)
    {
        base.Write(value);
        if (AutoFlush) Flush();
    }

    public override void Write(char[] buffer,int index,int count)
    {
        base.Write(buffer,index,count);
        if (AutoFlush) Flush();
    }
}

android-如何使用kivy从TextInput写入并保存到文本文件

android-如何使用kivy从TextInput写入并保存到文本文件

我想在TextInput小部件中输入文本以将其保存到文本文件中.请有人给我展示一个示例,该示例如何获取在TextInput小部件中输入的值以将其保存到文本文件中.

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
import os

此方法是尝试保存到文本文件中的尝试,但是没有用

def save(self,nam):
    fob = open('c:/test.txt','w')
    write =    fob.write(str(name))


Builder.load_string('''
<MenuScreen>:
    BoxLayout:
        Button:
            text: 'Add New Staff'
            on_press: root.manager.current = 'add_staff'
        Button:
            text: 'View Staff Profile'
        Button:
            text: 'Salary report'

<Add_new_staff>:
    nam: str(name_input)
    job: job_input
    GridLayout:
        cols: 2 
        Label:
            text: 'Name'
        TextInput:
            id: name_input
            multiline: False
        Label:
            text: 'Job'
        TextInput:
            id: job_input
        Label:
            text: 'Salary'
        TextInput:
        Label:
            text: 'Date of Joining'
        TextInput:
        Button:
            text: 'Back to menu'
            on_press: root.manager.current = 'menu'
        Button:
            text: 'Save'
            on_press: app.save(self,nam)
''')


class MenuScreen(Screen):
    pass

class Add_new_staff(Screen):
    pass

sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(Add_new_staff(name='add_staff'))

class TestApp(App):
    def build(self):
        return sm



if __name__ == '__main__':
    TestApp().run()

解决方法:

这是您的示例工作.

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput


Builder.load_string('''
<MenuScreen>:
    BoxLayout:
        Button:
            text: 'Add New Staff'
            on_press: root.manager.current = 'add_staff'
        Button:
            text: 'View Staff Profile'
        Button:
            text: 'Salary report'

<Add_new_staff>:
    nam: str(name_input)
    job: job_input
    GridLayout:
        cols: 2
        Label:
            text: 'Name'
        TextInput:
            id: name_input
            multiline: False
        Label:
            text: 'Job'
        TextInput:
            id: job_input
        Label:
            text: 'Salary'
        TextInput:
        Label:
            text: 'Date of Joining'
        TextInput:
        Button:
            text: 'Back to menu'
            on_press: root.manager.current = 'menu'
        Button:
            text: 'Save'
            on_press: app.save(name_input.text, job_input.text)
''')


class MenuScreen(Screen):
    pass

class Add_new_staff(Screen):
    pass

sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(Add_new_staff(name='add_staff'))

class TestApp(App):
    def build(self):
        return sm

    def save(self, name, job):
        fob = open('c:/test.txt','w')
        fob.write(name + "\n")
        fob.write(job)
        fob.close()    

if __name__ == '__main__':
    TestApp().run()

但是有几点建议.
 1.而是使用数据库(sqlite3?)存储此类数据.它将更有效地扩展,在数据变大时为您提供更快的查找.
 2.将您的数据存储在所有用户的“读/写”位置. Kivy为此提供了便利功能.

http://kivy.org/docs/api-kivy.app.html?highlight=data_dir#kivy.app.App.user_data_dir

希望有帮助吗?

干杯

asp.net写入日志到文本文件

asp.net写入日志到文本文件

[csharp] view plain copy

  1. using System;  

  2. using System.Collections.Generic;  

  3. using System.Web;  

  4. using System.IO;  

  5. using System.Text;  

  6.   

  7. /// <summary>  

  8. /// Summary description for NetLog  

  9. /// </summary>  

  10. public class NetLog  

  11. {  

  12.     /// <summary>  

  13.     /// 写入日志到文本文件  

  14.     /// </summary>  

  15.     /// <param name="action">动作</param>  

  16.     /// <param name="strMessage">日志内容</param>  

  17.     /// <param name="time">时间</param>  

  18.     public static void WriteTextLog(string action, string strMessage, DateTime time)  

  19.     {  

  20.         string path = AppDomain.CurrentDomain.BaseDirectory + @"System\Log\";  

  21.         if (!Directory.Exists(path))  

  22.             Directory.CreateDirectory(path);  

  23.   

  24.         string fileFullPath = path + time.ToString("yyyy-MM-dd") + ".System.txt";  

  25.         StringBuilder str = new StringBuilder();  

  26.         str.Append("Time:    " + time.ToString() + "\r\n");  

  27.         str.Append("Action:  " + action + "\r\n");  

  28.         str.Append("Message: " + strMessage + "\r\n");  

  29.         str.Append("-----------------------------------------------------------\r\n\r\n");  

  30.         StreamWriter sw;  

  31.         if (!File.Exists(fileFullPath))  

  32.         {  

  33.             sw = File.CreateText(fileFullPath);  

  34.         }  

  35.         else  

  36.         {  

  37.             sw = File.AppendText(fileFullPath);  

  38.         }  

  39.         sw.WriteLine(str.ToString());  

  40.         sw.Close();  

  41.     }  

  42. }  



BufferedWriter 不会将 Map 的内容写入文本文件;我的世界服务器

BufferedWriter 不会将 Map 的内容写入文本文件;我的世界服务器

好的,所以有多个问题。我没有控制缓冲区的刷新,我错误地声明了 hasmap,我没有访问哈希映射的值部分,我没有正确强制执行纯文本。

解决方案

public static Map <UUID,PVPstats> sPVPStats = new HashMap<UUID,PVPstats>();
    public static void writePVPStats() throws IOException {
        
        BufferedWriter w = new BufferedWriter(new FileWriter("plugins/core/killstats.txt"));
        
        for (PVPstats object: sPVPStats.values()) {
            try {
                System.out.println(sPVPStats);
                System.out.println(object.toString());
                
                w.write(object.toString() + "\n");
                w.flush();
                
              } catch (IOException e) {
                  throw new UncheckedIOException(e);
              }
        };
        w.close();
    }

我们今天的关于使用Writer将int写入文本文件用write方法写入字符串到指定的文件里的分享就到这里,谢谢您的阅读,如果想了解更多关于.net – 是否有TextWriter子类在写入文本时触发事件?、android-如何使用kivy从TextInput写入并保存到文本文件、asp.net写入日志到文本文件、BufferedWriter 不会将 Map 的内容写入文本文件;我的世界服务器的相关信息,可以在本站进行搜索。

本文标签: