Scala和案例类(Scala and case classes)
我正在阅读文件,并将行号附加到每个文件。 下面是一个列表来帮助这个例子:
val testList: List[String] = List("Dont", "Do", "It"
)val output: List[(String, Int)] = (testList.zipWithIndex)
我的程序使用._1&._2访问器获得了一点代码嗅觉。 我创建:
case class File(line: String, lineNum: Int)
但是,我知道如何最好地利用这个案例类的唯一方法是使用以下内容:
val fileOutput: List[File] = for{(line, lineNum) <- output} yield{File(line, lineNum)}
我的问题:为什么我不能这样做?
val output: List[File] = (testList.zipWithIndex)
为了使用案例课程,我对我的文件做了两个通行证感到厌倦。
提前致谢
I am reading in a file, and appending line numbers to each file. Below is a List to facilitate this example:
val testList: List[String] = List("Dont", "Do", "It"
)val output: List[(String, Int)] = (testList.zipWithIndex)
My program is getting a bit of code-smell with using the ._1 & ._2 accessors. I created:
case class File(line: String, lineNum: Int)
However, the only way I know how to make best use of this case class would be to use the following:
val fileOutput: List[File] = for{(line, lineNum) <- output} yield{File(line, lineNum)}
My question: why can't I do this?
val output: List[File] = (testList.zipWithIndex)
I'm a bit weary of doing two passes on my file for the sake of using a case-class.
Thanks in advance
原文:https://stackoverflow.com/questions/40393617
最满意答案
使用FileStream的代码:
byte[] buffer = new byte[1024]; int numberOfBytesRead = 0; FileStream fs = new FileStream(@"C:\file.png", FileMode.Create, FileAccess.Write); do { numberOfBytesRead = serverStream.Read(buffer, 0, buffer.Length); //Read from network stream fs.Write(buffer, 0, numberOfBytesRead); } while (serverStream.DataAvailable); fs.Close();
Code using FileStream :
byte[] buffer = new byte[1024]; int numberOfBytesRead = 0; FileStream fs = new FileStream(@"C:\file.png", FileMode.Create, FileAccess.Write); do { numberOfBytesRead = serverStream.Read(buffer, 0, buffer.Length); //Read from network stream fs.Write(buffer, 0, numberOfBytesRead); } while (serverStream.DataAvailable); fs.Close();
相关问答
更多-
除非.net使用的不是winsock,否则根据winsock参考: 发送功能的成功完成并不表示数据已成功发送并已收到收件人。 此功能仅表示数据已成功发送。 如果传输系统中没有可用的缓冲空间来保存要传输的数据,则发送将被阻塞,除非套接字已被置于非阻塞模式。 在非阻塞的面向流的套接字上,写入的字节数可以在1和请求的长度之间,具体取决于客户端和服务器计算机上的缓冲区可用性。 假设write是在底层调用send,那么对winsock文档的严格解释就会表明没有保证数据在返回时将其传递到管道的另一端。 这里是我引用的w ...
-
NetworkStream没有文件概念,它只是流式字节缓冲区的一个类。 您需要做的是提出某种形式的协议来将文件名发送到客户端。 您可以通过首先传输文件名和文件名的长度,然后传输文件大小,然后传输实际的文件内容来完成此操作。 客户端可以读取文件名长度,读入文件名,读入文件大小然后读入文件数据。 NetworkStream has no concept of files it's just a class for streaming byte buffers. What you will have to do ...
-
问题可能如下: bw.Write(new FileInfo(fileLocation).Length); ... var dataLength = br.ReadInt32(); Length属性实际上是long类型(8字节)。 但是您正在将值读取为Int32 (4个字节),而在流中保留其他4个字节。 The problem could be the following: bw.Write(new FileInfo(fileLocation).Length); ... var dataLength = b ...
-
这些是包装流 。 您可以围绕任何现有流创建GzipStream ,以将压缩数据读取或写入内部流。 These are wrapper streams. You can create a GzipStream around any existing stream to read or write compressed data to the inner stream.
-
它看起来像你的客户端代码没有完成阅读整个内容:你应该有一个循环,在检查内容之前读到最后: int remaining = d.Length; int pos = 0; while (remaining != 0) { int add = networkStream.Read(d, pos, remaining); pos += add; remaining -= add; } 目前,您的代码将尽可能多地读取网络流选择的数据,以便最初提供给您,并停止检索剩余的字节。 It looks ...
-
如何更改我的网络流代码以将数据直接写入文件(How to change my networkstream code to write the data directly to file)[2024-03-09]
使用FileStream的代码: byte[] buffer = new byte[1024]; int numberOfBytesRead = 0; FileStream fs = new FileStream(@"C:\file.png", FileMode.Create, FileAccess.Write); do { numberOfBytesRead = serverStream.Read(buffer, 0, buffer.Length) ... -
C#从Networkstream读取(C# Read from a Networkstream)[2022-07-18]
在您的服务器代码中,您忽略了NetworkStream.Read的结果,这是读取的实际字节数。 这可能小于您实际请求的字节数。 您需要继续调用Read,直到您收到所需的所有字节为止。 您在1次或多次写入之间看到的差异与网络堆栈中的缓冲/定时有关。 我怀疑你在单次写作时很幸运地收到了图像 - 但情况并非总是如此! 如上所述,您需要处理Read的结果。 代码示例: void ReadBuffer(byte[] buffer, int offset, int count) { int num; i ... -
MSDN描述应该更好地理解为 Write方法阻塞,直到请求的字节数写入本地网络缓冲区或抛出SocketException 即写入将在另一端成功接收整个文件之前返回。 这也意味着当您关闭应用程序时,可能会继续发送当前在网络缓冲区中的任何内容。 阻止主线程直到成功接收整个文件的唯一方法是可能使用异步套接字,一旦发送完成,等待接收端发送某种确认,您必须实现。 The MSDN description should perhaps be better read as The Write method blocks ...
-
我不确定我是否看到了你的所有代码,但这里有一个例子,如果我要使用TcpSockets而不是WCF,我会怎么做。 服务器端 public void StartServer() { TcpListener listener = null; TcpClient client = null; NetworkStream nwStream = null; try { listener = new TcpListener(IPAddress.Any, 4444 ...
-
NetworkStream缺少数据(NetworkStream missing data)[2023-05-23]
你忽略了Read的返回值。 不要那样做。 Read不会等到它读取您请求的所有数据。 您应该循环阅读,直到您阅读了所需的一切: byte[] data = new byte[dataSize]; int index = 0; while (index < dataSize) { int bytesRead = ns.Read(data, index, dataSize - index); if (bytesRead <= 0) { // Or whatever exce ...