FileStream
How do I use File Stream
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
Line | Code | Ex.. |
---|---|---|
74 | FileStream file = null; | 1 |
98 | FileStream file = null; | 2 |
83 | FileStream file = null; | 3 |
107 | FileStream file = null; | 4 |
72 | FileStream fs = new FileStream(@"chat.log", FileMode.Append, FileAccess.Write); | 5 |
File name: GameController.cs
Copy
73 public void Save(){
74 FileStream file = null;
75
76 try{
77 BinaryFormatter bf = new BinaryFormatter();
78 file = File.Create(Application.persistentDataPath + "/data.dat");
79
80 if(file != null){
81 data.SetIsGameStartedFirstTime(isGameStartedFirstTime);
82 data.SetIsMusicOn(isMusicOn);
83 data.SetCoins (coins);
84 data.SetWeaponLevel (weaponLevel);
85 bf.Serialize(file, data);
86 }
87
88 }catch(Exception e){
89 Debug.LogException (e, this);
90 }finally{
91 if(file != null){
92 file.Close ();
93 }
94 }
95 }
File name: GameController.cs
Copy
97 public void Load(){
98 FileStream file = null;
99
100 try{
101 BinaryFormatter bf = new BinaryFormatter();
102 file = File.Open(Application.persistentDataPath + "/data.dat", FileMode.Open);
103 data = bf.Deserialize(file) as GameData;
104 }catch(Exception e){
105 Debug.LogException (e, this);
106 }finally{
107 if(file != null){
108 file.Close ();
109 }
110 }
111 }
File name: GameController.cs
Copy
82 public void Save(){
83 FileStream file = null;
84
85 try{
86 BinaryFormatter bf = new BinaryFormatter();
87 file = File.Create(Application.persistentDataPath + "/data.dat");
88
89 if(data != null){
90 data.SetIsMusicOn(isMusicOn);
91 data.SetIsGameStartedFirstTime(isGameStartedFirstTime);
92 data.SetHighScore(highscore);
93 data.SetLevels(levels);
94 bf.Serialize(file, data);
95 }
96
97 }catch(Exception e){
98 Debug.LogException (e, this);
99 }finally{
100 if(file != null){
101 file.Close ();
102 }
103 }
104 }
File name: GameController.cs
Copy
106 public void Load(){
107 FileStream file = null;
108
109 try{
110 BinaryFormatter bf = new BinaryFormatter();
111 file = File.Open(Application.persistentDataPath + "/data.dat", FileMode.Open);
112 data = bf.Deserialize(file) as GameData;
113
114 }catch(Exception e){
115 Debug.LogException (e, this);
116 }finally{
117 if(file != null){
118 file.Close ();
119 }
120 }
121 }
File name: ChatBot.cs
Copy
52 private void showOutput()
53 {
54 if (!(string.IsNullOrWhiteSpace(InputTxt.Text))) // Make sure the textbox isnt empty
55 {
56 SoundPlayer Send = new SoundPlayer("SOUND1.wav"); // Send Sound Effect
57 SoundPlayer Rcv = new SoundPlayer("SOUND2.wav"); // Recieve Sound Effect
58
59 // Show the user message and play the sound
60 addInMessage(InputTxt.Text);
61 Send.Play();
62
63 // Store the Bot's Output by giving it our input.
64 string outtt = bot.getOutput(InputTxt.Text);
65
66 if (outtt.Length == 0)
67 {
68 outtt = "I don't understand.";
69 }
70
71 //=========== Creates backup of chat from user and bot to the given location ============
72 FileStream fs = new FileStream(@"chat.log", FileMode.Append, FileAccess.Write);
73 if (fs.CanWrite)
74 {
75 byte[] write = System.Text.Encoding.ASCII.GetBytes(InputTxt.Text + Environment.NewLine + outtt + Environment.NewLine);
76 fs.Write(write, 0, write.Length);
77 }
78 fs.Flush();
79 fs.Close();
80 //=======================================================================================
81
82 // Make a Dynamic Timer to delay the bot's response to make it feel humanlike.
83 var t = new Timer();
84
85 // Time in milseconds - minimum delay of 1s plus 0.1s per character.
86 t.Interval = 1000 + (outtt.Length * 100);
87
88 // Show the "Bot is typing.." text
89 txtTyping.Show();
90
91 // disable the chat box white the bot is typing to prevent user spam.
92 InputTxt.Enabled = false;
93
94 t.Tick += (s, d) =>
95 {
96 // Once the timer ends
97
98 InputTxt.Enabled = true; // Enable Chat box
99
100 // Hide the "Bot is typing.." text
101 txtTyping.Hide();
102
103 // Show the bot message and play the sound
104 addOutMessage(outtt);
105 Rcv.Play();
106
107 // Text to Speech if enabled
108 if (textToSpeech)
109 {
110 reader.SpeakAsync(outtt);
111 }
112
113 InputTxt.Focus(); // Put the cursor back on the textbox
114 t.Stop();
115 };
116 t.Start(); // Start Timer
117
118 InputTxt.Text = ""; // Reset textbox
119 }
120 }
FileStream 142 lượt xem
Gõ tìm kiếm nhanh...